问一个查询语句,急(在线等)

ou630j 2009-04-06 02:21:30

folder
字段 全部为varchar(50)
rowid(子节点编号),pid(父节点编号),rowname(子节点名称)
1 0 山东
2 1 青岛
3 1 大连
4 0 广东
5 4 广州
6 3 大连公司
7 6 部门
8 7 小李

现在我想查询 小李(8)这个节点下面所有的父节点
得到
7 6 部门
6 3 大连公司
3 1 大连
1 0 山东



请问怎么写这个语句 急等
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-04-06
  • 打赏
  • 举报
回复
/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间: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



多个的使用临时表组合数据,参考:








SQL code

/*
标题:查询所有顶级节点及其子节点的例
地址:http://topic.csdn.net/u/20090323/21/63a91f51-c4df-464d-ba18-64343deb4e3a.html
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-03-23
地点:广东深圳
*/

[code=SQL]create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
go

select * from area

drop table area

/*
id Name order_by father_ID
----------- ---------- ----------- -----------
1 广东省 2 0
2 四川省 2 0
3 湖北省 2 0
4 东莞市 1 1
5 广州市 1 1
6 天河区 0 5
7 绵阳市 1 2
8 武汉市 1 3
9 汉口区 0 8
10 随州市 1 3

(所影响的行数为 10 行)

要求显示为:
name
--------------
广东省
东莞市
广州市
天河区
四川省
绵阳市
湖北省
武汉市
汉口区
随州市

(所影响的行数为 10 行)
*/



SQL code

--创建原始表
create table Area (id int identity,Name varchar(10) ,order_by int ,father_ID int )
insert into area values('广东省',2,0)
insert into area values('四川省',2,0)
insert into area values('湖北省',2,0)
insert into area values('东莞市',1,1)
insert into area values('广州市',1,1)
insert into area values('天河区',0,5)
insert into area values('绵阳市',1,2)
insert into area values('武汉市',1,3)
insert into area values('汉口区',0,8)
insert into area values('随州市',1,3)
--创建临时表
create table tmp (id int identity,Name varchar(10) ,order_by int ,father_ID int )
go

--创建查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , 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 area a , @t_Level b
where a.father_ID = b.id and b.level = @level - 1
end
return
end
go

--创建存储过程并将数据插入临时表
create proc my_proc
as
begin
declare @id as int
set @id = 0
while exists(select 1 from area where order_by = 2 and id > @id)
begin
set @id = (select min(id) from area where order_by = 2 and id > @id)
insert into tmp(Name ,order_by ,father_ID) select a.name,a.order_by ,a.father_id from area a , f_cid(@id) b where a.id = b.id order by a.id
end
end
go
exec my_proc

--从临时表提取数据并显示
select case when order_by = 2 then name
when order_by = 1 then ' ' + name
when order_by = 0 then ' ' + name
end name
from tmp order by id

drop function f_cid
drop proc my_proc
drop table area , tmp

/*
name
--------------
广东省
东莞市
广州市
天河区
四川省
绵阳市
湖北省
武汉市
汉口区
随州市

(所影响的行数为 10 行)

*/
ws_hgo 2009-04-06
  • 打赏
  • 举报
回复
查老帖BOM
yygyogfny 2009-04-06
  • 打赏
  • 举报
回复


create table folder
(id int,pid int,rowname nvarchar(40))

insert into folder
select 1,0,'山东'
union all
select 2,1,'青岛'
union all
select 3,1,'大连'
union all
select 4,0,'广东'
union all
select 5,4,'广州'
union all
select 6,3,'大连公司'
union all
select 7,6,'部门'
union all
select 8,7,'小李'

with cte(id,pid,rowname)
as
(
select a.id,a.pid,a.rowname from folder a where a.id = 8
union all
select b.id,b.pid,b.rowname from folder b inner join cte on b.id = cte.pid
)

select * from cte

id pid rowname
----------- ----------- ----------------------------------------
8 7 小李
7 6 部门
6 3 大连公司
3 1 大连
1 0 山东

(5 row(s) affected)
liangCK 2009-04-06
  • 打赏
  • 举报
回复
CREATE FUNCTION f_Pid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @ID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.PID,@Level
FROM tb a,@t_Level b
WHERE a.ID=b.ID
AND b.Level=@Level-1
END
RETURN
END
GO


--上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。
CREATE FUNCTION f_Pid(@ID char(3))
RETURNS @t_Level TABLE(ID char(3))
AS
BEGIN
INSERT @t_Level SELECT @ID
SELECT @ID=PID FROM tb
WHERE ID=@ID
AND PID IS NOT NULL
WHILE @@ROWCOUNT>0
BEGIN
INSERT @t_Level SELECT @ID
SELECT @ID=PID FROM tb
WHERE ID=@ID
AND PID IS NOT NULL
END
RETURN
END
Zoezs 2009-04-06
  • 打赏
  • 举报
回复



---------------------------------
-- Author: liangCK 小梁
---------------------------------

--> 生成测试数据: tb
CREATE TABLE tb(ID INT,ParentID INT,Name VARCHAR(1))
INSERT INTO tb
SELECT 1,0,'A' UNION ALL
SELECT 2,1,'B' UNION ALL
SELECT 3,1,'C' UNION ALL
SELECT 4,2,'D' UNION ALL
SELECT 5,0,'E' UNION ALL
SELECT 6,4,'F' UNION ALL
SELECT 7,2,'G'

--SQL查询如下:

DECLARE @MaxLevel INT;

WITH Liang AS
(
SELECT *,level=1 FROM tb WHERE ParentID=0
UNION ALL
SELECT A.*,level+1
FROM tb AS A
JOIN Liang AS B ON A.ParentID=B.ID
)
SELECT @MaxLevel=MAX(level)
FROM Liang;

DECLARE @col VARCHAR(MAX);
SET @col='';

SELECT
@col=@col+',Name'+RTRIM(number)
FROM master.dbo.spt_values
WHERE type='p' AND number BETWEEN 1 AND @MaxLevel;

SET @col=STUFF(@col,1,1,'');

EXEC ('
WITH Liang AS
(
SELECT *,level=1,PATH=CAST(Name AS VARCHAR(MAX)) FROM tb WHERE ParentID=0
UNION ALL
SELECT A.*,level+1,CAST(B.Path+'',''+A.name AS VARCHAR(MAX))
FROM tb AS A
JOIN Liang AS B ON A.ParentID=B.ID
),
Liang2 AS
(
SELECT
A.*,
SUBSTRING(A.Path,B.number,CHARINDEX('','',A.Path+'','',B.number)-B.number) AS v,
B.number
FROM Liang AS A
JOIN master.dbo.spt_values AS B
ON B.type=''p'' AND number BETWEEN 1 AND LEN(A.Path)
AND SUBSTRING('',''+A.Path,B.number,1)='',''
),
Liang3 AS
(
SELECT
*,
rname=CAST(''name''+RTRIM(ROW_NUMBER() OVER(PARTITION BY ID ORDER BY number)) AS VARCHAR(20))
FROM Liang2
)
SELECT *
FROM (
SELECT ID,ParentID,Name,rname,v FROM Liang3
) AS A
PIVOT (MAX(v) FOR rname IN('+@col+')) AS pvt
');

DROP TABLE tb

/*
ID ParentID Name Name1 Name2 Name3 Name4
--- ------ ---------- ------- ------ ------- -------
1 0 A A NULL NULL NULL
2 1 B A B NULL NULL
3 1 C A C NULL NULL
4 2 D A B D NULL
5 0 E E NULL NULL NULL
6 4 F A B D F
7 2 G A B G NULL
*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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