注册 登录
编程论坛 ASP技术论坛

查询语句怎么写

s6gy 发布于 2007-11-20 19:10, 451 次点击

只有本站会员才能查看附件,请 登录


ID

ID NAME ID1
1 重要工作 0
2 通知 1
3 工作动态 1
4 动态 3
也就是上面的显示的1是父类 2,3是1子类而4又是3的子类


这样显示的时候怎么查询啊
希望能写一下谢谢




[此贴子已经被作者于2007-11-20 19:11:49编辑过]

4 回复
#2
purana2007-11-20 19:30

//在sqlserver里测试成功.

[CODE]declare @t table(ID int,Name nvarchar(50),ID1 int)
insert into @t values(1,N'重要工作',0)
insert into @t values(2,N'通知',1)
insert into @t values(3,N'工作动态',1)
insert into @t values(4,N'动态',3)
declare @t_Level table(ID int,Level int,Sort varchar(8000))
declare @Level int
set @Level=0
insert @t_Level select ID,@Level,cast(ID as varchar)
from @t
where ID1=0
while @@rowcount>0
begin
set @Level=@Level+1
insert @t_Level select a.ID,@Level,b.Sort+cast(a.ID as varchar)
from @t a,@t_Level b
where a.ID1=b.ID and b.Level=@Level-1
end
select space(b.Level*2)+'|--'+a.Name
from @t a,@t_Level b
where a.ID=b.ID
order by b.Sort
/*
|--重要工作
|--通知
|--工作动态
|--动态
(所影响的行数为 4 行)
*/[/CODE]

#3
s6gy2007-11-21 08:44

能否写 得简单一点哦怎么看得不是嘿明白
对存储过程好象不是很了解 谢谢

#4
piaoxue2007-11-21 09:53
怎么查询 要看你数据录里面的数据是怎么写的了。 一般情况下 直接查询ID就可以了呀。
#5
purana2007-11-21 10:31
这种东西..一般很难只靠Select查询..
写成一个存储过程..调用就好了..
网上调用存储过程的例子很多..看一下就知道了..
1