mysql每日一题0719- 省份地区县层级显示
省份地区县层级显示
hope you can enjoy 😄
WITH temp AS (
SELECT a.id,a.CODE,a.NAME,a.parentcode,b.NAME name1,b.CODE code1 FROM `t0719` a LEFT JOIN `t0719` b ON a.CODE=b.parentcode)
SELECT a.NAME name_1,a.name1 name_2,b.NAME name_3
FROM temp a
LEFT JOIN temp b ON a.code1=b.parentcode
WHERE a.parentcode=0
UNION
SELECT a.NAME name_1,NULL AS name_2,NULL AS name_3
FROM temp a WHERE a.parentcode=0 ORDER BY name_1,name_2,name_3