这里写目录标题
- 一、pgsql
- 二、mysql
一、pgsql
WITH RECURSIVE sub_categories AS (SELECT id, parent_id, nameFROM categoriesWHERE parent_id = '父级ID' -- 替换为你要查询的父级IDUNION ALLSELECT c.id, c.parent_id, c.nameFROM categories cINNER JOIN sub_categories sc ON sc.id = c.parent_id
)
SELECT * FROM sub_categories;
二、mysql
SELECT*
FROM(SELECTt1.*,IF( FIND_IN_SET( parent_id, @parent_ids ) > 0, @parent_ids := CONCAT( @parent_ids, ',', id ), '0' ) AS ischild FROM( SELECT * FROM blade_menu AS t WHERE t.is_deleted = 0 AND client_type = 'dianduyun_app' ORDER BY t.id ASC ) t1,( SELECT @parent_ids := '1508255373140103170' ) t2 ) t3
WHEREischild != '0'
需要修改的地方
- parent_id,改为你的父级id字段
- blade_menu ,改为你的表名称
- WHERE t.is_deleted = 0 ,改为你的查询条件
- @parent_ids := ‘1508255373140103170’,值改为你要传入的父级id