请教一个获得所有上级部门的sql语句,希望尽可能优化

cyc123007512 cyc123007512 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
create table testdept(
deptid int identity(1,1) primary key,
deptname varchar(16),
superdept int
);


insert into testdept(deptname,superdept) values('总经办',0);
insert into testdept(deptname,superdept) values('财务部',1);
insert into testdept(deptname,superdept) values('工程部',1);
insert into testdept(deptname,superdept) values('会计部',2);
insert into testdept(deptname,superdept) values('出纳部',2);
insert into testdept(deptname,superdept) values('北京工程办',3);
insert into testdept(deptname,superdept) values('南京工程办',3);

希望查询出这样的结果:
deptid deptname superdept
0 总经办 总经办
1 工程部 总经办
2 财务部 总经办
3 会计部 财务部
4 出纳部 财务部
5 北京工程办 工程部
6 南京工程办 工程部



...全文

相关推荐

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