Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 正在另一个字符串中查找子字符串_Sql_Sql Server_Substring - Fatal编程技术网

Sql 正在另一个字符串中查找子字符串

Sql 正在另一个字符串中查找子字符串,sql,sql-server,substring,Sql,Sql Server,Substring,我有两个MS SQL表: 我必须用循环来说明,如果表1的字符串的所有字符都包含在表2的字符串中 例如 希望你能帮忙 谢谢 --编辑 下划线是分隔符 下面是结果的样子: 您可以尝试以下方法: DECLARE @xml1 xml, @xml2 xml SELECT @xml1= ( SELECT (CAST('<b id="'+cast(id as nvarchar(2)) + '"><a>' + REPLACE(string,'_','</a><a&g

我有两个MS SQL表:

我必须用循环来说明,如果表1的字符串的所有字符都包含在表2的字符串中

例如

希望你能帮忙

谢谢

--编辑

下划线是分隔符

下面是结果的样子:

您可以尝试以下方法:

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
如我所见,
\u a\u b\u
中包含了
\u a\u c\u b\u d\u

您可以尝试以下方法:

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

正如我所见,
\u a_b_
中包含了
\u a_c_b_d_

两个表中包含行的示例数据将有助于解释您尝试执行的操作。不清楚您想要什么结果。下划线是分隔符还是输入的一部分?请使用校验和将表1和表2对齐!!校验和不起作用选择校验和(“'u a_b_c'),校验和('u b_c_a')产生不同的输出,但OP希望这是真的您似乎有错误的数据格式。如果以字符串形式存储列表,则应修复基础数据以使用连接表。两个表中的行示例数据将有助于解释您试图执行的操作。不清楚您想要什么结果。下划线是分隔符还是输入的一部分?请使用校验和将表1和表2对齐!!校验和不起作用选择校验和(“'u a_b_c'),校验和('u b_c_a')产生不同的输出,但OP希望这是真的您似乎有错误的数据格式。如果以字符串形式存储列表,则应修复基础数据以使用连接表。
table1id    table2id
----------- -----------
1           1
2           1
2           4
4           1
4           3

(5 row(s) affected)