如何在多列上搜索列表?
How to search a list on multiple columns?
我有一个包含多个项目的临时 table。
我想在一秒钟内搜索多个列 table 以获取包含此项目但具有 LIKE 函数的行:
例如
#list = temp table with items
SELECT *
FROM table2
WHERE
ID like %#list%
OR Name like %#list%
OR Adress like %#list%
这可以用 TSQL 实现吗?
我建议您使用 EXISTS
:
SELECT *
FROM table2 t2
WHERE EXISTS (SELECT 1
FROM #List L
WHERE t2.ID LIKE CONCAT('%',L.Item,'%') --Assumes ID is a string based data type
OR t2.Name LIKE CONCAT('%',L.Item,'%')
OR t2.Adress LIKE CONCAT('%',L.Item,'%')); --Address has 2 d's
我找到了一个很好的答案。
将您要处理的字符串放入#temp table
与“%text1%”
items
%text1%
%text2%
然后使用 JOIN 进行搜索,例如:
SELECT *
FROM table_1
INNER JOIN #temp
ON table_1.column1 like #temp.items
OR table_1.column2 like #temp.items
...
我觉得效果不错
我有一个包含多个项目的临时 table。
我想在一秒钟内搜索多个列 table 以获取包含此项目但具有 LIKE 函数的行:
例如
#list = temp table with items
SELECT *
FROM table2
WHERE
ID like %#list%
OR Name like %#list%
OR Adress like %#list%
这可以用 TSQL 实现吗?
我建议您使用 EXISTS
:
SELECT *
FROM table2 t2
WHERE EXISTS (SELECT 1
FROM #List L
WHERE t2.ID LIKE CONCAT('%',L.Item,'%') --Assumes ID is a string based data type
OR t2.Name LIKE CONCAT('%',L.Item,'%')
OR t2.Adress LIKE CONCAT('%',L.Item,'%')); --Address has 2 d's
我找到了一个很好的答案。
将您要处理的字符串放入#temp table 与“%text1%”
items |
---|
%text1% |
%text2% |
然后使用 JOIN 进行搜索,例如:
SELECT *
FROM table_1
INNER JOIN #temp
ON table_1.column1 like #temp.items
OR table_1.column2 like #temp.items
...
我觉得效果不错