我的表结构如下:id name parentID
1 a 0
2 b 0
3 a1 1
4 a2 1
5 b1 2
6 b2 2要实现的结果如下:
id name parentID
1 a 0
3 a1 1
4 a2 1
2 b 0
5 b1 2
6 b2 2就是按照大类排序,子类跟在父类的后面,这种排序方式如何查询?或者有其他的方式吗?谢谢
1 a 0
2 b 0
3 a1 1
4 a2 1
5 b1 2
6 b2 2要实现的结果如下:
id name parentID
1 a 0
3 a1 1
4 a2 1
2 b 0
5 b1 2
6 b2 2就是按照大类排序,子类跟在父类的后面,这种排序方式如何查询?或者有其他的方式吗?谢谢
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001',NULL ,'山东省'
UNION ALL SELECT '002','001','烟台市'
UNION ALL SELECT '004','002','招远市'
UNION ALL SELECT '003','001','青岛市'
UNION ALL SELECT '005',NULL ,'四会市'
UNION ALL SELECT '006','005','清远市'
UNION ALL SELECT '007','006','小分市'--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
FROM @t a,@t_Level b
WHERE a.PID=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
/*--结果
|--山东省
|--烟台市
|--招远市
|--青岛市
|--四会市
|--清远市
|--小分市
--*/
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华
时间:2008-05-12
地点:广东深圳
*/create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 10 行)
*/--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区(所影响的行数为 2 行)
*/--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇(所影响的行数为 7 行)
*/drop table tb
drop function f_cid
from ta
order by case when parentID = 0 then id else parentid end,id
-- Author: happyflystone
-- Version:V1.001
-- Date:2008-09-11 17:21:53
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(id int,name nvarchar(2),parentID nvarchar(1))
Go
Insert into ta
select 1,'a','0' union all
select 2,'b','0' union all
select 3,'a1','1' union all
select 4,'a2','1' union all
select 5,'b1','2' union all
select 6,'b2','2'
Go
--Start
Select *
from ta
order by case when parentID = 0 then id else parentid end,id--Result:
/*
id name parentID
----------- ---- --------
1 a 0
3 a1 1
4 a2 1
2 b 0
5 b1 2
6 b2 2(所影响的行数为 6 行)*/
--End
declare @table table (id int identity(1,1), name varchar(8), parentID int)insert into @table values ('a',0)
insert into @table values ('b',0)
insert into @table values ('a1',1)
insert into @table values ('a2',1)
insert into @table values ('b1',2)
insert into @table values ('b2',2)declare @id2code table (id int, name varchar(8), code varchar(64))
-- Root级别
insert into @id2code
select id, name, code=cast(id as varchar)
from @table
where parentID=0while exists (select 1
from @table a
left join @id2code b on a.id=b.id
where b.id is null) begin
insert into @id2code
select a.id, a.name, b.code+'.'+cast(a.id as varchar)
from @table a
join @id2code b on a.parentID=b.id
endselect * from @id2code order by code
-- id name code
-- 1 a 1
-- 3 a1 1.3
-- 4 a2 1.4
-- 2 b 2
-- 5 b1 2.5
-- 6 b2 2.6
--