我有两个MS SQL表:
如果第一表的字符串中的所有字符都包含在表二的字符串中,我必须说一个循环。
E.g。
(_a_b_c_d_) is contained in (_a_c_b_d_)
(_a_b_) is contained in (_g_b_a_)
(_a_f_d_) is not contained in (_a_c_b_d_)
希望你能帮忙!
由于
- 编辑
下划线是分隔符
这里的结果如下:
答案 0 :(得分:0)
你可以试试这个:
DECLARE @xml1 xml, @xml2 xml
SELECT @xml1= (
SELECT (CAST('<b id="'+cast(id as nvarchar(2)) + '"><a>' + REPLACE(string,'_','</a><a>') + '</a></b>' as xml))
from table1
FOR XML PATH('')
)
SELECT @xml2= (
SELECT (CAST('<b id="'+cast(id as nvarchar(2)) + '"><a>' + REPLACE(string,'_','</a><a>') + '</a></b>' as xml))
from table2
FOR XML PATH('')
)
;WITH res1 AS (
SELECT t.v.value('../@id','int') as id,
t.v.value('.','char(1)') as chars
FROM @xml1.nodes('/b/a') as t(v)
WHERE t.v.value('.','char(1)') !=''
), res2 AS (
SELECT t.v.value('../@id','int') as id,
t.v.value('.','char(1)') as chars
FROM @xml2.nodes('/b/a') as t(v)
WHERE t.v.value('.','char(1)') !=''
), cte1 AS (
SELECT distinct id, (SELECT '_' + r.chars FROM res1 r WHERE r.id = r1.id ORDER BY r.id, r.chars FOR XML PATH('')) + '_' as string
FROM res1 r1
), cte2 AS (
SELECT distinct id, (SELECT '_' + r.chars FROM res2 r WHERE r.id = r2.id ORDER BY r.id, r.chars FOR XML PATH('')) + '_' as string
FROM res2 r2
)
SELECT t1.id as table1id, t2.id as table2id
FROM cte1 t1
INNER JOIN cte2 t2 ON t2.string LIKE '%'+t1.string+'%' --t1.string LIKE '%'+t2.string+'%'
ORDER BY t1.id
结果:
table1id table2id
----------- -----------
1 1
2 1
2 4
4 1
4 3
(5 row(s) affected)
我可以看到_a_b_
中的_a_c_b_d_
已被提及。