请问高手,怎么写这个SQL语句?
产品表:
PID -- 主键
PName -- 产品名称
PParentID -- 如果该值非0则表示这个产品属于子产品,该值对应PID产品名称为本产品的父产品.
如何通过一条SQL查询出以下的结果集
PID 产品A
PID 产品A (1)
PID 产品A (2)
PID 产品B
PID 产品B (1)
PID 产品B (2)
PID 产品B (3)
谢谢,盼高手出手相助,急.
问题点数:50、回复次数:10Top
1 楼playyuer(退休干部 卧鼠藏虫)回复于 2003-12-03 21:56:24 得分 10
树型结构数据在数据库基本表中的存储及维护
http://www.csdn.net/Develop/Read_Article.asp?Id=18666
一道 SQL 题 ... (关于树型结构的在关系表中的存储及其应用处理)
http://www.csdn.net/Develop/Read_Article.asp?Id=17247
http://www.csdn.net/Develop/list_article.asp?author=playyuer
Top
2 楼pengdali()回复于 2003-12-03 21:57:00 得分 0
select * from 产品表 order by case when PParentID=0 then PID else PParentID endTop
3 楼pengdali()回复于 2003-12-03 21:57:21 得分 10
[树]
http://expert.csdn.net/Expert/TopicView1.asp?id=2285830Top
4 楼stevejobs(小康)回复于 2003-12-03 22:07:20 得分 0
to: pengdali(大力 V3.0)
你写的那SQL语句中 order by case...?
不太明白~,似乎行不通啊,可能我不明你的原理,能解释一下吗?
to: playyuer(双规干部)
哎,紧,只好一会再看你留的地址了.谢谢你.
哎,想不到在这一留言,两位五星大侠就出手相助,感激不尽!
好人一生平安.Top
5 楼stevejobs(小康)回复于 2003-12-04 07:21:37 得分 0
UPTop
6 楼zjcxc(邹建)回复于 2003-12-04 08:59:04 得分 0
--树形数据的处理
--自定义函数,得到编码累计及级别表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getbmmerg]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getbmmerg]
GO
create function f_getbmmerg()
returns @re table(id int,idmerg varchar(8000),level int)
as
begin
--为了数字排序正常,需要统一编码宽度
declare @idlen int,@idheader varchar(20)
select @idlen=max(len(PID)),@idheader=space(@idlen) from 产品表
declare @level int
set @level=1
insert into @re select PID,right(@idheader+cast(PID as varchar),@idlen),@level
from 产品表 where PParentID=0
while @@rowcount>0
begin
set @level=@level+1
insert into @re select b.PID,a.idmerg+','+right(@idheader+cast(b.PID as varchar),@idlen),@level
from @re a inner join 产品表 b on a.id=b.PParentID
where a.level=@level-1
end
return
end
go
--调用上面的自定义实现你的排序要求:
select a.* from 产品表 a join dbo.f_getbmmerg() b on a.pid=b.id
order by b.idmergTop
7 楼zjcxc(邹建)回复于 2003-12-04 09:03:39 得分 30
--下面是数据测试
--测试数据
create table 产品表(PID int identity(1,1) primary key,PName varchar(10),PParentID int)
insert into 产品表
select '产品A',0
union all select '产品B',0
union all select '产品A1',1
union all select '产品A2',1
union all select '产品B1',2
union all select '产品B2',2
union all select '产品A11',3
union all select '产品A12',3
union all select '产品B21',6
union all select '产品B22',6
--树形数据的处理
--自定义函数,得到编码累计及级别表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getbmmerg]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getbmmerg]
GO
create function f_getbmmerg()
returns @re table(id int,idmerg varchar(8000),level int)
as
begin
--为了数字排序正常,需要统一编码宽度
declare @idlen int,@idheader varchar(20)
select @idlen=max(len(PID)),@idheader=space(@idlen) from 产品表
declare @level int
set @level=1
insert into @re select PID,right(@idheader+cast(PID as varchar),@idlen),@level
from 产品表 where PParentID=0
while @@rowcount>0
begin
set @level=@level+1
insert into @re select b.PID,a.idmerg+','+right(@idheader+cast(b.PID as varchar),@idlen),@level
from @re a inner join 产品表 b on a.id=b.PParentID
where a.level=@level-1
end
return
end
go
--调用得到结果
select a.* from 产品表 a join dbo.f_getbmmerg() b on a.pid=b.id
order by b.idmerg
go
--删除测试环境
drop table 产品表
drop function f_getbmmerg
/*--测试结果
PID PName PParentID
----------- ---------- -----------
1 产品A 0
3 产品A1 1
7 产品A11 3
8 产品A12 3
4 产品A2 1
2 产品B 0
5 产品B1 2
6 产品B2 2
9 产品B21 6
10 产品B22 6
(所影响的行数为 10 行)
--*/Top
8 楼zjcxc(邹建)回复于 2003-12-04 09:04:51 得分 0
参考我的贴子:
树形数据处理
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.4361994Top
9 楼weasea(尘一笑)回复于 2003-12-04 09:12:38 得分 0
仔细看一下,这么多星Top
10 楼stevejobs(小康)回复于 2003-12-04 10:02:04 得分 0
OK,感谢各位老大出手相助.
谢谢 zjcxc(邹建)
谢谢 pengdali(大力 V3.0)
谢谢 playyuer(双规干部)
Top




