求接sql语句

xiaoxuefengwu 2009-09-24 10:43:26
表A,结构如下:

--------------------------
id txt parentId
1 a 0
2 b 0
3 c 1
4 d 1
5 e 2
--------------------------


想要的结果:
--------------------------

id txt parentId txtb
1 a 0 c
1 a 0 d
2 b 0 e

--------------------------


也就是说先选出parentId=0,然后增加一项数据txtb 是A表中parentId=id时的txt的值。。



求语句!!



...全文
55 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
虫洞 2009-09-25
  • 打赏
  • 举报
回复
使用树形结构与CTE实现父子列多层次查询http://blog.csdn.net/canhui87/archive/2009/09/18/4566042.aspx
dawugui 2009-09-25
  • 打赏
  • 举报
回复
如果只有两级,如上写的即可.如果是多级,就麻烦了.


/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间: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



@@ROWCOUNT:返回受上一语句影响的行数。
返回类型:integer。
注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。

UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
IF @@ROWCOUNT = 0
print 'Warning: No rows were updated'

结果:

(所影响的行数为 0 行)
Warning: No rows were updated



/*
标题:查询所有节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-04-12
地点:广东深圳
*/

--生成测试数据
create table tb(id varchar(10),pid varchar(10))
insert into tb select 'a', null
insert into tb select 'b', 'a'
insert into tb select 'c', 'a'
insert into tb select 'd', 'b'
insert into tb select 'e', 'b'
insert into tb select 'f', 'c'
insert into tb select 'g', 'c'
go

--创建用户定义函数
create function f_getchild(@id varchar(10)) returns varchar(8000)
as
begin
declare @i int , @ret varchar(8000)
declare @t table(id varchar(10) , pid varchar(10) , level int)
set @i = 1
insert into @t select id , pid , @i from tb where id = @id
while @@rowcount <> 0
begin
set @i = @i + 1
insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
end
select @ret = isnull(@ret , '') + id + ',' from @t
return left(@ret , len(@ret) - 1)
end
go

--执行查询
select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
go

--输出结果
/*
id children
---------- -------------
a a,b,c,d,e,f,g
b b,d,e
c c,f,g
d d
e e
f f
g g

(所影响的行数为 7 行)

*/

--删除测试数据
drop function f_getchild
drop table tb
xiaoxuefengwu 2009-09-25
  • 打赏
  • 举报
回复
谢谢大家!
lihan6415151528 2009-09-25
  • 打赏
  • 举报
回复

select a.id ,a.txt,a.parentId,b.txt as txtb from tb a,tb b
where a.id=b.parentId
and a.parentId=0

--小F-- 2009-09-24
  • 打赏
  • 举报
回复
--------写反了

----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-24 22:47:09
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[txt] varchar(1),[parentId] int)
insert [tb]
select 1,'a',0 union all
select 2,'b',0 union all
select 3,'c',1 union all
select 4,'d',1 union all
select 5,'e',2
--------------开始查询--------------------------


;with f as
(
select a.id,b.[txt] from tb a ,tb b where a.id=b.[parentId]
)
select f.id,t.txt,t.parentId ,f.txt as txtb from (select * from tb where [parentId]=0)t left join f on t.id=f.id
----------------结果----------------------------
/* id txt parentId txtb
----------- ---- ----------- ----
1 a 0 c
1 a 0 d
2 b 0 e

(3 行受影响)
*/
Zoezs 2009-09-24
  • 打赏
  • 举报
回复

select a.id ,a.txt,a.parentId,b.txt as txtb
from tb a,tb b
where a.id=b.parentId
and a.parentId=0
--小F-- 2009-09-24
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-09-24 22:47:09
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[txt] varchar(1),[parentId] int)
insert [tb]
select 1,'a',0 union all
select 2,'b',0 union all
select 3,'c',1 union all
select 4,'d',1 union all
select 5,'e',2
--------------开始查询--------------------------


;with f as
(
select a.id,b.[txt] from tb a ,tb b where a.id=b.[parentId]
)
select f.id,f.txt,t.parentId ,t.txt as txtb from (select * from tb where [parentId]=0)t left join f on t.id=f.id
----------------结果----------------------------
/* id txt parentId txtb
----------- ---- ----------- ----
1 c 0 a
1 d 0 a
2 e 0 b

(3 行受影响)

*/
华夏小卒 2009-09-24
  • 打赏
  • 举报
回复

IF OBJECT_ID('tb') IS NOT NULL DROP TABLE tb
Go
create table tb(id int, txt char(1), parentId int)
insert tb select
1, 'a' , 0 union all select
2 , 'b' , 0 union all select
3 , 'c' , 1 union all select
4 , 'd' , 1 union all select
5 , 'e', 2

select a.id ,a.txt,a.parentId,b.txt as txtb from tb a,tb b
where a.id=b.parentId
and a.parentId=0


id txt parentId txtb
----------- ---- ----------- ----
1 a 0 c
1 a 0 d
2 b 0 e

(3 行受影响)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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