0

我从来没有用过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.

谢谢

4

1 回答 1

2

为了快速搜索多列,您可以在 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"');

返回包含任何搜索列中的任何单词的行。检查这个答案

对于 contains 和 freetext 检查Sql Server Full-Text Search Protips Part 2: CONTAINS vs. FREETEXT文章之间的区别

于 2013-03-07T09:18:12.810 回答