1、2、3楼的方法我曾想过。 但要知道用自增id取值考虑到一个问题.就是关联id不一定按照我说的进行规则写出 例如: id name sign 1 a 0 2 b 1 3 c 1 4 d 11 5 e 3 6 f 5 通过 select * from tb where id > = 3 而其中11 XX 0 关联的是 1 a 0 7 g 6 。。 。。 。。 11 XX 0
难道sql 无法实现吗? 看来真要从程序入手了。
Create table T (id int,name varchar(10),[sign] int) insert into T select 1,'a',0 insert into T select 2,'b',1 insert into T select 3,'c',1 insert into T select 4,'d',3 insert into T select 5,'e',3 insert into T select 6,'f',5 insert into T select 7,'g',6 GoCreate function f_cid(@id int) returns @t table(id int,level int) as begin declare @level int set @level=1 insert into @t select @id,@level while @@rowcount>0 begin set @level=@level+1 insert into @t select a.[id],@level from T a,@t b where a.[sign]=b.[id] and b.level=@level-1 end return endGOselect A.id,name from dbo.f_cid(3) as A,T where A.id=T.id /* id name -------------- 3 c 4 d 5 e 6 f 7 g*/ drop table T drop function dbo.f_cid
如果没有
select * from tb where id >= 3
但要知道用自增id取值考虑到一个问题.就是关联id不一定按照我说的进行规则写出
例如:
id name sign
1 a 0
2 b 1
3 c 1
4 d 11
5 e 3
6 f 5 通过 select * from tb where id > = 3 而其中11 XX 0 关联的是 1 a 0
7 g 6
。。
。。
。。
11 XX 0
看来真要从程序入手了。
Create table T (id int,name varchar(10),[sign] int)
insert into T select 1,'a',0
insert into T select 2,'b',1
insert into T select 3,'c',1
insert into T select 4,'d',3
insert into T select 5,'e',3
insert into T select 6,'f',5
insert into T select 7,'g',6
GoCreate function f_cid(@id int)
returns @t table(id int,level int)
as
begin
declare @level int
set @level=1
insert into @t select @id,@level
while @@rowcount>0
begin
set @level=@level+1
insert into @t select a.[id],@level
from T a,@t b
where a.[sign]=b.[id]
and b.level=@level-1
end
return
endGOselect A.id,name from dbo.f_cid(3) as A,T
where A.id=T.id
/*
id name
--------------
3 c
4 d
5 e
6 f
7 g*/
drop table T
drop function dbo.f_cid
所以自增id没有规则,通过自增id来实现 有些不妥
至于lz講的子增id,跟那個function沒關系
现在揭贴