主从关系俩个表查询,难度较大,不是普通的那种关联

wxl2ooo 2010-09-21 11:39:12
A表
aa bb
kk 1,2,3,4,5
mm 22,32,44,4
nn 55,4,43,22,34

B表

id info
1 a
2 b
3 c
4 d
5 e
22 f
32 g
44 h
55 i

B表中的id=A表中bb用逗号隔开的各个值

如何查询到 包括A表和B表的所有内容

例如结果:kk 1 a
kk 2 b
kk 3 c
kk 4 d
kk 5 e
mm 22 f
mm 32 g
mm 44 h
mm 4 d
...全文
88 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wxl2ooo 2010-09-22
[Quote=引用 10 楼 ws_hgo 的回复:]
SQL code
if object_id('A') is not null drop table A
go
create table A
(
aa varchar(20),
bb varchar(50)
)
insert into A select 'kk','1,2,3,4,5'
union all select 'mm','22,32,44,4'
union al……
[/Quote]

这个方法是很不错,不需要临时表,临时变量表,ws_hgo对字符串的处理很强大哦

顺便问一下,如果A表中有俩个字段都是:

bb字段22,32,44,4
cc字段1,2,2,1,1

sql语句改如何处理才能对应一致呢
回复
王向飞 2010-09-21
看见带逗号的就闹心
回复
Andy__Huang 2010-09-21
同意一楼,感觉表设计不合理

回复
ws_hgo 2010-09-21
if object_id('A') is not null drop table A
go
create table A
(
aa varchar(20),
bb varchar(50)
)
insert into A select 'kk','1,2,3,4,5'
union all select 'mm','22,32,44,4'
union all select 'nn','55,4,43,22,34'
go


select distinct a.aa,substring(a.bb,number,charindex(',',a.bb+',',P.number)-number) bb
from A a
join master..spt_values P
on p.type='P'
and substring(','+a.bb,p.number,1)=','

aa bb
-------------------- --------------------------------------------------
kk 1
kk 2
kk 3
kk 4
kk 5
mm 22
mm 32
mm 4
mm 44
nn 22
nn 34
nn 4
nn 43
nn 55

(14 行受影响)


回复
dawugui 2010-09-21
现在才看懂题意,是要全部存在才显示。(需要进行数据拆分。)
create table A(aa varchar(10),bb varchar(20))
insert into a values('kk', '1,2,3,4,5')
insert into a values('mm', '22,32,44,4')
insert into a values('nn', '55,4,43,22,34')
create table B(id int,info varchar(10))
insert into b values(1 ,'a')
insert into b values(2 ,'b')
insert into b values(3 ,'c')
insert into b values(4 ,'d')
insert into b values(5 ,'e')
insert into b values(22 ,'f')
insert into b values(32 ,'g')
insert into b values(44 ,'h')
insert into b values(55 ,'i')
go

--方法1.使用临时表完成
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b

select a.* , b.info from a , b where charindex(','+cast(b.id as varchar) +',',',' + a.bb+',') > 0
and a.aa not in (select distinct aa from
(
SELECT A.aa, bb = SUBSTRING(A.[bb], B.id, CHARINDEX(',', A.[bb] + ',', B.id) - B.id)
FROM A, # B
WHERE SUBSTRING(',' + A.[bb], B.id, 1) = ','
) t where bb not in (select id from b))

DROP TABLE #
drop table a , b

/*
aa bb info
---------- -------------------- ----------
kk 1,2,3,4,5 a
kk 1,2,3,4,5 b
kk 1,2,3,4,5 c
kk 1,2,3,4,5 d
kk 1,2,3,4,5 e
mm 22,32,44,4 d
mm 22,32,44,4 f
mm 22,32,44,4 g
mm 22,32,44,4 h

(所影响的行数为 9 行)

*/



有关拆分的其他内容见下:
/*
标题:简单数据拆分(version 2.0)
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-05-07
地点:重庆航天职业学院
描述:

有表tb, 如下:
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
欲按id,分拆value列, 分拆后结果如下:
id value
----------- --------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
*/

--1. 旧的解决方法(sql server 2000)

create table tb(id int,value varchar(30))
insert into tb values(1,'aa,bb')
insert into tb values(2,'aaa,bbb,ccc')
go

--方法1.使用临时表完成
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b

SELECT A.id, value = SUBSTRING(A.[value], B.id, CHARINDEX(',', A.[value] + ',', B.id) - B.id)
FROM tb A, # B
WHERE SUBSTRING(',' + A.[value], B.id, 1) = ','

DROP TABLE #

--方法2.如果数据量小,可不使用临时表
select a.id , value = substring(a.value , b.number , charindex(',' , a.value + ',' , b.number) - b.number)
from tb a join master..spt_values b
on b.type='p' and b.number between 1 and len(a.value)
where substring(',' + a.value , b.number , 1) = ','

--2. 新的解决方法(sql server 2005)
create table tb(id int,value varchar(30))
insert into tb values(1,'aa,bb')
insert into tb values(2,'aaa,bbb,ccc')
go

--方法1.使用xml完成
SELECT A.id, B.value FROM
(
SELECT id, [value] = CONVERT(xml,'<root><v>' + REPLACE([value], ',', '</v><v>') + '</v></root>') FROM tb
) A OUTER APPLY
(
SELECT value = N.v.value('.', 'varchar(100)') FROM A.[value].nodes('/root/v') N(v)
) B

--方法2.使用CTE完成
;with tt as
(select id,[value]=cast(left([value],charindex(',',[value]+',')-1) as nvarchar(100)),Split=cast(stuff([value]+',',1,charindex(',',[value]+','),'') as nvarchar(100)) from tb
union all
select id,[value]=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from tt where split>''
)
select id,[value] from tt order by id option (MAXRECURSION 0)


DROP TABLE tb

/*
id value
----------- ------------------------------
1 aa
1 bb
2 aaa
2 bbb
2 ccc

(5 行受影响)
*/



回复
黑子白子 2010-09-21
设计没法改的话,就用表变量或临时表缓存吧,先按,分割,单个数字存入表变量,之后再select
回复
黑子白子 2010-09-21
A表设计上就不应该使用','分割,而采用子表结构
回复
dawugui 2010-09-21
create table A(aa varchar(10),bb varchar(20))
insert into a values('kk', '1,2,3,4,5')
insert into a values('mm', '22,32,44,4')
insert into a values('nn', '55,4,43,22,34')
create table B(id int,info varchar(10))
insert into b values(1 ,'a')
insert into b values(2 ,'b')
insert into b values(3 ,'c')
insert into b values(4 ,'d')
insert into b values(5 ,'e')
insert into b values(22 ,'f')
insert into b values(32 ,'g')
insert into b values(44 ,'h')
insert into b values(55 ,'i')
go

select a.* , b.* from a , b where charindex(','+cast(b.id as varchar) +',',',' + a.bb+',') > 0

select a.* , b.* from a , b where ',' + a.bb+',' like '%,'+cast(b.id as varchar) +',%'

drop table a , b

/*
aa bb id info
---------- -------------------- ----------- ----------
kk 1,2,3,4,5 1 a
kk 1,2,3,4,5 2 b
kk 1,2,3,4,5 3 c
kk 1,2,3,4,5 4 d
kk 1,2,3,4,5 5 e
mm 22,32,44,4 4 d
mm 22,32,44,4 22 f
mm 22,32,44,4 32 g
mm 22,32,44,4 44 h
nn 55,4,43,22,34 4 d
nn 55,4,43,22,34 22 f
nn 55,4,43,22,34 55 i

(所影响的行数为 12 行)
*/
回复
wxl2ooo 2010-09-21
现在就是因为设计没办法更改了,所以才想办法解决问题的,务必请大家帮帮忙
回复
dawugui 2010-09-21
select a.* , b.* from a , b where charindex(','+cast(a.id as varchar) +',',',' + b.bb+',') > 0
select a.* , b.* from a , b where ',' + b.bb+',' like '%,'+cast(a.id as varchar) +',%'
回复
zsh0809 2010-09-21
A表
aa bb
kk 1,2,3,4,5
mm 22,32,44,4
nn 55,4,43,22,34
思路:
主要就是将A表数据拆分成纵向表
如:kk 1
kk 2
.....
就可以了。
回复
设计有问题,不想写
回复
wxl2ooo 2010-09-21
大致的方法明白了,谢谢大家,也谢谢那些认为设计不合理,不愿意帮忙解决的人
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2010-09-21 11:39
社区公告
暂无公告