求一SQL语句不知道如何的写

lu仙深 2011-08-11 09:34:34
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)
...全文
181 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
不放糖的咖啡 2011-08-11
  • 打赏
  • 举报
回复
where t1.ta=t2.tb

=》

where t1.ta=t2.tb and t1.ta!=t2.ta
AcHerat 元老 2011-08-11
  • 打赏
  • 举报
回复
你就不能把商品的tb改成-1 。。。。 -2 什么的?
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
4 1 一卡通设备
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品
1 1 商品


消息 530,级别 16,状态 1,第 1 行
语句被终止。完成执行语句前已用完最大递归 100。
cd731107 2011-08-11
  • 打赏
  • 举报
回复
create table #tb(TA int,TB int,TC varchar(16))

insert #tb
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 行受影响)
*/
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
1 商品 1
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


我改成其他了,也是一样的
Billy 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lwcvod 的回复:]
消息 530,级别 16,状态 1,第 1 行
语句被终止。完成执行语句前已用完最大递归 100。



这是为什么呢??
[/Quote]
那是因为ta=tb=0导致死循环(默认100次),所以改'商品'的TB值为其他,
AcHerat 元老 2011-08-11
  • 打赏
  • 举报
回复

;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
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
消息 530,级别 16,状态 1,第 1 行
语句被终止。完成执行语句前已用完最大递归 100。



这是为什么呢??
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xyctc 的回复:]
SQL code

;with tt
as
(
select * from tb where tc='Dell'
union all
select t.* from tb t1,tt t2 where t1.ta=t2.tb
)
select * from tt
[/Quote]





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。


不知道为什么会递归这么多行
Billy 2011-08-11
  • 打赏
  • 举报
回复
;with tt
as
(
select * from tb where tc='Dell'
union all
select t.* from tb t1,tt t2 where t1.ta=t2.tb
)
select * from tt
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
这个是一个树结构
就是通过,最末的查出,它的父节点出来

0 0 商品
32 0 电脑
33 32 笔记本
38 33 DELL
快溜 2011-08-11
  • 打赏
  • 举报
回复
select * from tb where ta in(38,33) or tc in('DELL')
NBDBA 2011-08-11
  • 打赏
  • 举报
回复
写个函数取路径吧,楼下写吧
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
给出 38 33 DELL

TA=38 查出


读取出 (商品 电脑 笔记本 DELL)

chuanzhang5687 2011-08-11
  • 打赏
  • 举报
回复
select * from tb where tc in ('商品', '电脑' ,'笔记本' ,'DELL')
闹铃 2011-08-11
  • 打赏
  • 举报
回复
不懂 需求不明确
NBDBA 2011-08-11
  • 打赏
  • 举报
回复
说清楚规则

好像是树形结构,取最后一个?
chmwl00544 2011-08-11
  • 打赏
  • 举报
回复
create table #tb(TA int,TB int,TC varchar(16))

insert #tb
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

自连接查询
lu仙深 2011-08-11
  • 打赏
  • 举报
回复
给搞晕了,用 《cd731107》办法,商品=0 就行了

我成别的了,就不行了

现在OK了,结贴

感谢各位的帮忙

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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