查询本级及上级
-- 查询本级及上级
SELECT t2.id, t2.category_name
FROM (
SELECT
@r AS _id,
(SELECT @r := parent_id FROM lib_material_category WHERE id = _id) AS parent_id,
@l := @l - 1 AS le
FROM
(SELECT @r := 3, @l := 3) vars,
lib_material_category h
WHERE @r <> 0) t1
JOIN lib_material_category t2
ON t1._id = t2.id
ORDER BY t1.le DESC;
查询本级及下级 仅查询下级 if(t1.id= 1,0,-1) 修改为 -1
-- 查询本级及下级 仅查询下级 if(t1.id= 1,0,-1) 修改为 -1
SELECT
id
FROM
(
SELECT t1.id, IF ( find_in_set( parent_id, @ids ) > 0, @ids := concat( @ids, ',', id ), if(t1.id= 1,0,-1) ) AS ischild
FROM
( SELECT id, parent_id FROM lib_material_category t WHERE t.del_flag = '0' ORDER BY parent_id, id ) t1,( SELECT @ids := 1 ) t2
) t3
WHERE
ischild != -1
;