代码改变世界

mysql查询直接下级和所有下级

2021-06-10 17:58  VinpleZhang  阅读(1937)  评论(0编辑  收藏  举报

SELECT id
FROM (
SELECT *
FROM ty_hotel_product
WHERE parent_product_id > 0

ORDER BY parent_product_id DESC
) realname_sorted, (
SELECT @pv := 2
) initialisation
WHERE FIND_IN_SET(parent_product_id, @pv) > 0
AND (@pv := concat(@pv, ',', id))




select id from (
select t1.id,
if(find_in_set(parent_product_id, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild
from (
select id,parent_product_id from ty_hotel_product t where t.status = 1 order by parent_product_id, id
) t1,
(select @pids := 1) t2
) t3 where ischild != 0