请教一个Sql语句的写法

yangniao 2012-06-04 04:04:12
表 department
字段 id name parentid
值 1 销售部 0
2 市场部 0
3 人事部 0
4 张三 1
5 张一 1
6 李四 2
7 王五 2

现在想要查询出来的效果是这样的
字段 id name parentid
值 1 销售部 0
4 张三 1
5 张一 1
2 市场部 0
6 李四 2
7 王五 2
3 人事部 0
就是ID为1的下边显示parentid为1的值
这样的sql语句要怎么写
...全文
130 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangniao 2012-06-04
  • 打赏
  • 举报
回复
能重新搞下不 上面是表
yangniao 2012-06-04
  • 打赏
  • 举报
回复
create table testp
(
id int identity(1,1) primary key,
tname varchar(100),
parentid int ,
)
insert into testp(tname,parentid) values('销售部',0)
insert into testp(tname,parentid) values('市场部',0)
insert into testp(tname,parentid) values('人事部',0)
insert into testp(tname,parentid) values('张三',1)
insert into testp(tname,parentid) values('张一',1)
insert into testp(tname,parentid) values('李四',2)
insert into testp(tname,parentid) values('王五',3)
insert into testp(tname,parentid) values('张力',3)
select * from testp
不啦草 2012-06-04
  • 打赏
  • 举报
回复
select * from #table1 就是你要的结果
不啦草 2012-06-04
  • 打赏
  • 举报
回复
with #table1 as(select * from department
union all
select Department.* from Department, #table1 where #table1.id = Department.parentid)

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧