TA TB TC
0 0 商品
25 0 通讯设备
26 31 打印设备
27 0 一卡通设备
28 0 监控设备
29 0 防盗设备
30 0 网络设备
31 0 办公设备
32 0 电脑
33 32 笔记本
34 32 台式机
35 34 HP
36 33 IBM
37 33 HP
38 33 DELL各位大侠,这个SQL 如何的写啊?
读取出 (商品 电脑 笔记本 DELL)
0 0 商品
25 0 通讯设备
26 31 打印设备
27 0 一卡通设备
28 0 监控设备
29 0 防盗设备
30 0 网络设备
31 0 办公设备
32 0 电脑
33 32 笔记本
34 32 台式机
35 34 HP
36 33 IBM
37 33 HP
38 33 DELL各位大侠,这个SQL 如何的写啊?
读取出 (商品 电脑 笔记本 DELL)
读取出 (商品 电脑 笔记本 DELL)
就是通过,最末的查出,它的父节点出来0 0 商品
32 0 电脑
33 32 笔记本
38 33 DELL
as
(
select * from tb where tc='Dell'
union all
select t.* from tb t1,tt t2 where t1.ta=t2.tb
)
select * from tt
SuppliersID SuppliersID_Parent Suppliers_TYPE Suppliers_ID Suppliers_TYPE_PY TYPE_YONG_TU
38 33 DELL 02 DELL 0
33 32 笔记本 01 BJB 0
32 0 电脑 07 DN 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0
0 0 商品 NULL NULL 0消息 530,级别 16,状态 1,第 1 行
语句被终止。完成执行语句前已用完最大递归 100。
不知道为什么会递归这么多行
;with ach as
(
select * from tb where tc='DELL'
union all
select t.* from tb t join ach b on t.ta = b.tb
)select * from ach
2 通讯设备 1
4 一卡通设备 1
5 监控设备 1
6 防盗设备 1
7 网络设备 1
8 办公设备 1
9 电脑 1
10 笔记本 9
11 台式机 9
12 HP 11
13 IBM 10
14 HP 10
15 DELL 10
我改成其他了,也是一样的
select 0 ,0 ,'商品' union all
select 25 ,0 ,'通讯设备' union all
select 26 ,31 ,'打印设备' union all
select 27 ,0 ,'一卡通设备' union all
select 28 ,0 ,'监控设备' union all
select 29 ,0 ,'防盗设备' union all
select 30 ,0 ,'网络设备' union all
select 31 ,0 ,'办公设备' union all
select 32 ,0 ,'电脑' union all
select 33 ,32 ,'笔记本' union all
select 34 ,32 ,'台式机' union all
select 35 ,34 ,'HP' union all
select 36 ,33 ,'IBM' union all
select 37 ,33 ,'HP' union all
select 38 ,33 ,'DELL';with cte as
(
select *,cast(tc as varchar(max)) as td from #tb where TA=38
union all
select b.*,b.tc+'-'+a.td from cte a,#tb b where a.TB=b.TA
and a.ta<>0
)select * from cte order by ta/*
TA TB TC td
----------- ----------- ---------------- ----------------------
0 0 商品 商品-电脑-笔记本-DELL
32 0 电脑 电脑-笔记本-DELL
33 32 笔记本 笔记本-DELL
38 33 DELL DELL(4 行受影响)
*/
select 0 ,0 ,'商品' union all
select 25 ,0 ,'通讯设备' union all
select 26 ,31 ,'打印设备' union all
select 27 ,0 ,'一卡通设备' union all
select 28 ,0 ,'监控设备' union all
select 29 ,0 ,'防盗设备' union all
select 30 ,0 ,'网络设备' union all
select 31 ,0 ,'办公设备' union all
select 32 ,0 ,'电脑' union all
select 33 ,32 ,'笔记本' union all
select 34 ,32 ,'台式机' union all
select 35 ,34 ,'HP' union all
select 36 ,33 ,'IBM' union all
select 37 ,33 ,'HP' union all
select 38 ,33 ,'DELL'
select d.tc as tc,c.tc as tc,b.tc as tc,a.tc as tc
from #tb a,#tb b,#tb c,#tb d
where
a.tc='dell' and
a.tb=b.ta and
b.tb=c.ta and
c.tb=d.ta and
a.tb<>0 and
b.tb<>0自连接查询