我从来没有用过PATINDEX()
,但我很难用PATINDEX()
. 通常我需要搜索任何表的多个列,然后我像这样编写 sql
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性能总是很好吗?我搜索 google 以获得更好的 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
请指导我上面的 sqlPATINDEX
语法可以用于搜索多列。如果没有,请指导我如何使用该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.
谢谢