如何查找包含两个单词 "First" + "Second" 的行,而不考虑顺序?
How do I find rows that contain two words "First" + "Second" regardless sequence?
#360Search #SearchAll #SearchContain #SearchEverything #SQL #SELECT
如何查找包含两个单词“First”+“Second”的行,无论顺序与否?正在寻找一个 where 条件来涵盖这种情况。
这是我的表格:
---------------------------
RowNo, MyStringColumn
---------------------------
1 , 'First Second Third'
2 , 'Second Third First'
3 , 'Third First Second'
4 , 'First Third Second'
5 , 'Second Second Third'
预期结果:第 1、2、3、4 行
---------------------------
RowNo, MyStringColumn
---------------------------
1 , 'First Second Third'
2 , 'Second Third First'
3 , 'Third First Second'
4 , 'First Third Second'
低于 SQL return第 1、3、4 行并且 return 由于序列无序而不是第 2 行。
SELECT * FROM MyTable WHERE MyStringColumn like '%First%Second%
这里是SQL生成MyTable和数据的脚本
DROP TABLE IF EXISTS [#MyTable];
SELECT [RowNo],[MyStringColumn] INTO [#MyTable]
FROM (SELECT (1)[RowNo], ('First Second Third')[MyStringColumn]
UNION SELECT 2, 'Second Third First'
UNION SELECT 3, 'Third First Second'
UNION SELECT 4, 'First Third Second'
UNION SELECT 5, 'Second Second Third'
)t
SELECT * FROM [#MyTable] WHERE [MyStringColumn] LIKE '%First%Second%'
只需 LIKE
两次。
SELECT *
FROM MyTable
WHERE MyStringColumn LIKE '%First%'
AND MyStringColumn LIKE '%Second%'
如果您想确保它是 2 个单独的词
SELECT *
FROM MyTable
WHERE ' '+MyStringColumn+' ' LIKE '% First %'
AND ' '+MyStringColumn+' ' LIKE '% Second %'
string_split
基于方法。当你想检查很多单词时很有用:
select *
from t
where exists (
select 1
from string_split(t.MyStringColumn, ' ')
where value in ('first', 'second')
having count(distinct value) = 2 -- this should match the number of items in the "in" clause
)
#360Search #SearchAll #SearchContain #SearchEverything #SQL #SELECT
如何查找包含两个单词“First”+“Second”的行,无论顺序与否?正在寻找一个 where 条件来涵盖这种情况。
这是我的表格:
---------------------------
RowNo, MyStringColumn
---------------------------
1 , 'First Second Third'
2 , 'Second Third First'
3 , 'Third First Second'
4 , 'First Third Second'
5 , 'Second Second Third'
预期结果:第 1、2、3、4 行
---------------------------
RowNo, MyStringColumn
---------------------------
1 , 'First Second Third'
2 , 'Second Third First'
3 , 'Third First Second'
4 , 'First Third Second'
低于 SQL return第 1、3、4 行并且 return 由于序列无序而不是第 2 行。
SELECT * FROM MyTable WHERE MyStringColumn like '%First%Second%
这里是SQL生成MyTable和数据的脚本
DROP TABLE IF EXISTS [#MyTable];
SELECT [RowNo],[MyStringColumn] INTO [#MyTable]
FROM (SELECT (1)[RowNo], ('First Second Third')[MyStringColumn]
UNION SELECT 2, 'Second Third First'
UNION SELECT 3, 'Third First Second'
UNION SELECT 4, 'First Third Second'
UNION SELECT 5, 'Second Second Third'
)t
SELECT * FROM [#MyTable] WHERE [MyStringColumn] LIKE '%First%Second%'
只需 LIKE
两次。
SELECT *
FROM MyTable
WHERE MyStringColumn LIKE '%First%'
AND MyStringColumn LIKE '%Second%'
如果您想确保它是 2 个单独的词
SELECT *
FROM MyTable
WHERE ' '+MyStringColumn+' ' LIKE '% First %'
AND ' '+MyStringColumn+' ' LIKE '% Second %'
string_split
基于方法。当你想检查很多单词时很有用:
select *
from t
where exists (
select 1
from string_split(t.MyStringColumn, ' ')
where value in ('first', 'second')
having count(distinct value) = 2 -- this should match the number of items in the "in" clause
)