我从未使用过PATINDEX()
,但我很难用PATINDEX()
搜索表数据。通常我需要搜索任意表的多列然后我写像
SELECT * FROM ADDRESS WHERE
((NAME LIKE 'Bill%') OR (CITY LIKE 'Bill%') OR (COMPANY LIKE 'Bill%'))
AND
((NAME LIKE 'Seattle%') OR (CITY LIKE 'Seattle%') OR (COMPANY LIKE 'Seattle%'))
所以只是告诉我上面我的sql性能总是很好吗?我搜索谷歌得到更好的sql搜索表的多列,发现下面的sql
select * from YourTable
WHERE PATINDEX('%text1%',COALESCE(field1,'') + '|' + COALESCE(field2,'') + '|'+ COALESCE(field3,'')+ '|' + COALESCE(field4,'')+ '|' + COALESCE(field9,''))>0
AND
PATINDEX('%text2%',COALESCE(field1,'') + '|' + COALESCE(field2,'') + '|'+ COALESCE(field3,'')+ '|' + COALESCE(field4,'')+ '|' +COALESCE(field9,''))>0
请指导我,以上sql PATINDEX
语法是可以搜索多列。如果没有,那么指导我如何使用PATINDEX
函数来搜索具有多个值的多列。由于
更新QS
你给了一个链接,告诉我如何搜索多个字段与多个关键字。在这里
SELECT FT_TBL.ProductDescriptionID,
FT_TBL.Description,
KEY_TBL.RANK
FROM Production.ProductDescription AS FT_TBL INNER JOIN
CONTAINSTABLE (Production.ProductDescription,
Description,
'(light NEAR aluminum) OR
(lightweight NEAR aluminum)'
) AS KEY_TBL
ON FT_TBL.ProductDescriptionID = KEY_TBL.[KEY]
WHERE KEY_TBL.RANK > 2
ORDER BY KEY_TBL.RANK DESC;
GO
但问题是理解起来有点复杂。你能给我SQL搜索多个字段与多个关键字与全文搜索。
我可以写这样的东西来搜索多个字段对多个值
SELECT Title
FROM Production.Document
WHERE FREETEXT (Document, '(vital) OR (safety) OR (components)')
SELECT *
FROM YourTable
WHERE CONTAINS((ProductName, ProductNumber, Color),
'(vital) OR (safety) OR (components)');
what is the difference between FREETEXT and CONTAINS ?? can u please explain.
谢谢
对于快速搜索多列,您可以在sql server上设置全文搜索那么你的查询就会像这样
SELECT * FROM ADDRESS
WHERE Contains((name, city, company), 'Bill') and Contains(*, 'Seattle')
编辑
此处描述的全文搜索查询语法
编辑
你可以写
SELECT *
FROM YourTable
WHERE CONTAINS((ProductName, ProductNumber, Color),
'"vital" OR "safety" OR "components"');
返回包含任何搜索列中的任何单词的行。检查这个答案
查看Sql Server全文检索提示第2部分:contains与freetext文章