select ID from (select t1.id, if(find_in_set(父级id字段名, @pids) > 0, @pids := concat(@pids, ',', 主键id字段名), -1) as ischild from ( select 主键id字段名,父级id字段名 from 表名 t order by 父级id字段名, 主键id字段名 ) t1, (select @pids := 需要查询的主键id) t2 ) t3 where ischild != -1
select ID from (select t1.id, if(find_in_set(父级id字段名, @pids) > 0, @pids := concat(@pids, ',', 主键id字段名), -1) as ischild from ( select 主键id字段名,父级id字段名 from 表名 t order by 父级id字段名, 主键id字段名 ) t1, (select @pids := 需要查询的主键id) t2 ) t3 where ischild != -1