一个难度很大的排序问题,求助!!!

JiangHongTao JiangHongTao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

id 为自身编号(自增长),pid 为父结点编号,porder 为在同一父结点下的排序号,sname 为地名
其实就是一棵多级树
create table #t (id int,pid int,poder int,sname varchar(100))
insert #t select 1,0,2,'中国'
union all select 2,0,1,'美国'
union all select 3,0,3,'英国'
union all select 4,1,2,'广东'
union all select 5,4,1,'深圳'
union all select 6,1,1,'上海'
union all select 7,4,2,'珠海'
union all select 8,0,4,'日本'
union all select 9,8,1,'东京'

请不要用函数和存储过程,最好是一条语句或几条语句搞定
要求输出:
id pid porder sname
2 0 1 '美国'
1 0 2 '中国'
6 1 1 '上海'
4 1 2 '广东'
5 4 1 '深圳'
7 4 2 '珠海'
3 0 3 '英国'
8 0 4 '日本'
9 8 1 '东京'
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
JiangHongTao
疑难问题 22,298 成员 121,727 内容