日期:2014-05-17 浏览次数:20422 次
用这个你想怎么关联?
SELECT id ,
SUBSTRING([qid], number, CHARINDEX(',', [qid] + ',', number) - number) AS [qid]
FROM table1 a ,
master..spt_values
WHERE number >= 1
AND number < LEN([qid])
AND type = 'p'
AND SUBSTRING(',' + [qid], number, 1) = ','
/*
id qid
----------- ----------------------------------------------------------------
1 1
2 1
2 2
2 3
3 2
3 3
*/
with wt as
(
SELECT A.id, B.qid
FROM(
SELECT id, [qid] = CONVERT(xml,'<root><v>' + REPLACE([qid], ',', '</v><v>') + '</v></root>') FROM table1
)A
OUTER APPLY(
SELECT qid = N.v.value('.', 'varchar(100)') FROM A.[qid].nodes('/root/v') N(v)
)B
)
select wt.id as table1id ,table2.id as table2id,table2.name from table2,wt
where table2.id = wt.qid and wt.qid <>''
select a.id,b.name from table1 a,table2 b where
a.qid like '%'+convert(varchar(50),b.id)+'%' order by a.id