java MySQL 查询所有子级(不包含自己)
别的不说上源码
parent_id :上级id
xun_village_region :表名
这条SQL只能查询到自己的下级
SELECT
t3.*
FROM
(
SELECT
t1.*,
IF
( find_in_set( parent_id, @pids ) > 0, @pids := concat( @pids, ',', id ), 0 ) AS ischild
FROM
( SELECT * FROM xun_village_region t where is_delete = 1 ORDER BY parent_id, id ) t1,
( SELECT @pids := #{parentId} ) t2
) t3
WHERE
ischild != 0