本文主要是介绍树级ParentId、childId查找上级(mysql),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图:
UPDATE eper_goods_category a
join eper_goods_category b on a.parent_code=b.category_code
join eper_goods_category c on b.parent_code=c.category_code
left join eper_goods_category d on c.parent_code=d.category_code
set a.category_code1=c.category_code,a.category_code2=b.category_code,a.category_code3=a.category_code
where d.id is null;
UPDATE eper_goods_category a
join eper_goods_category b on a.parent_code=b.category_code
left join eper_goods_category c on b.parent_code=c.category_code
left join eper_goods_category d on c.parent_code=d.category_code
set a.category_code1=b.category_code,a.category_code2=a.category_code
where d.id is null and c.id is null;
UPDATE eper_goods_category a
left join eper_goods_category b on a.parent_code=b.category_code
left join eper_goods_category c on b.parent_code=c.category_code
left join eper_goods_category d on c.parent_code=d.category_code
set a.category_code1=a.category_code
where d.id is null and c.id is null and b.id is null;
SELECT a.category_code1 as '一级code',a.category_code2 as '二级code',a.category_code3 as '三级code'
,b.category_name as '一级类目',c.category_name as '二级类目',d.category_name as '三级类目'
from eper_goods_category a
left join eper_goods_category b on a.category_code1=b.category_code
left join eper_goods_category c on a.category_code2=c.category_code
left join eper_goods_category d on a.category_code3=d.category_code
order by b.category_name,c.category_name,d.category_name
这篇关于树级ParentId、childId查找上级(mysql)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!