现有表:
id(不重复) 父级id 地区名字 级别
dist_id parent_id dist_name dist_type
1 0 中国 0
2 1 北京 1
52 2 北京 2
500 52 朝阳区 3
501 52 东城区 3
3 1 安徽 1
36 3 安庆 2
399 36 大观区 3
……………………………………………………
就是这样的表,省市区都在一张表里面,现在想查询出这样的:
id 父级id 省 市 区 级别
2 1 北京 1
52 2 北京 2
500 52 北京 朝阳区 3
501 52 北京 东城区 3
请各位大侠帮忙sql语句如何写才能实现这个结果,50高分悬赏
------解决方案--------------------
WITH test AS(
SELECT '1' dist_id,'0' parent_id,'中国' dist_name, '0' dist_type FROM dual UNION ALL
SELECT '2' dist_id,'1' parent_id,'北京' dist_name, '1' dist_type FROM dual UNION ALL
SELECT '52' dist_id,'2' parent_id,'北京' dist_name, '2' dist_type FROM dual UNION ALL