3

我正在创建一个 SQL 语句,用于在数据库中搜索特定列中的关键字。我需要查询返回符合关键字 1 和关键字 2 条件的记录。这工作得很好,但是我需要允许从多个列中查询关键字。添加 OR 子句后,无法让查询返回对两个关键字都命中的记录而不仅仅是一个关键字的结果。

为什么 OR 子句会影响 AND 子句?

如何修改此语句以允许在搜索指定的 3 列时需要两个关键字才能获得成功?

该声明:

SELECT CASE WHEN t1.longdesc IS NULL THEN t1.desc 
WHEN t1.longdesc IS NOT NULL THEN t1.longdesc END AS 'description', 
t1.upc 
FROM Items t1 
LEFT JOIN Suppliers t2 ON t1.supplier = t2.supplier_no 
LEFT JOIN Sections t3 ON t1.Section = t3.section_no 
LEFT JOIN Groups t4 on t1.group = t4.group 
WHERE desc LIKE '%keyword1%' 
OR Item_code LIKE '%keyword1%' 
OR certify_code LIKE '%keyword1%' 
AND desc LIKE '%keyword2%' 
OR Item_code LIKE '%keyword2%' 
OR certify_code LIKE '%keyword2%'
4

5 回答 5

3

AND的优先级高于OR。如果您想创建一个条件,逻辑上说“keyword1 在这些列中的任何一个上都匹配,而 keywrod2 在这些列中的任何一个上都匹配”,您需要AND用括号将每个 s 参数括起来以避免它优先:

(顺便说一句,选择列表中的描述表达式可以通过使用来简化coalesce

SELECT COALESCE (t1.longdesc, t1.desc) AS description, t1.upc 
FROM Items t1
LEFT JOIN Suppliers t2 ON t1.supplier = t2.supplier_no 
LEFT JOIN Sections t3 ON t1.Section = t3.section_no 
LEFT JOIN Groups t4 on t1.group = t4.group 
WHERE (desc LIKE '%keyword1%' OR 
       Item_code LIKE '%keyword1%' OR 
       certify_code LIKE '%keyword1%') AND 
      (desc LIKE '%keyword2%' OR 
       Item_code LIKE '%keyword2%' OR 
       certify_code LIKE '%keyword2%')
于 2015-06-10T22:47:59.733 回答
1

尝试这个:

SELECT 
    ISNULL(t1.longdesc, t1.[desc]) AS 'description'
    , t1.upc 
FROM Items t1 
LEFT JOIN Suppliers t2 
    ON t1.supplier = t2.supplier_no 
LEFT JOIN Sections t3 
    ON t1.Section = t3.section_no 
LEFT JOIN Groups t4 
    on t1.group = t4.group 
WHERE 
    ([desc] LIKE '%keyword1%' 
        OR Item_code LIKE '%keyword1%' 
        OR certify_code LIKE '%keyword1%') 
    AND (desc LIKE '%keyword2%' 
        OR Item_code LIKE '%keyword2%' 
        OR certify_code LIKE '%keyword2%')

我清理了您的 case 语句以ISNULL代替使用(coalesce也可以),并在您的 where 逻辑中添加了括号。

于 2015-06-10T22:47:48.913 回答
1

通过使用括号将您的WHERE部分更改为如下所示()

WHERE (
[desc] LIKE '%keyword1%' 
OR Item_code LIKE '%keyword1%' 
OR certify_code LIKE '%keyword1%' 
)
AND 
(
[desc] LIKE '%keyword2%' 
OR Item_code LIKE '%keyword2%' 
OR certify_code LIKE '%keyword2%'
)
于 2015-06-10T22:48:30.877 回答
1

您需要使用括号来使您的逻辑工作。见下文:

 SELECT
  CASE WHEN t1.longdesc IS NULL THEN t1.[desc]
       WHEN t1.longdesc IS NOT NULL THEN t1.longdesc
  END AS 'description',
  t1.upc
 FROM
  Items t1
  LEFT JOIN Suppliers t2
    ON t1.supplier = t2.supplier_no
  LEFT JOIN Sections t3
    ON t1.Section = t3.section_no
  LEFT JOIN Groups t4
    ON t1.[group] = t4.[group]
 WHERE
  (
    [desc] LIKE '%keyword1%'
    OR Item_code LIKE '%keyword1%'
    OR certify_code LIKE '%keyword1%'
  )
  AND (
        [desc] LIKE '%keyword2%'
        OR Item_code LIKE '%keyword2%'
        OR certify_code LIKE '%keyword2%'
      )
于 2015-06-10T22:50:47.513 回答
1

我建议你避免使用OR作为性能问题,并使用ISNULL(),所以你可以使用这个:

SELECT 
    ISNULL(t1.longdesc, t1.desc) AS 'description', 
    t1.upc 
FROM 
    Items t1 
LEFT JOIN 
    Suppliers t2 ON t1.supplier = t2.supplier_no 
LEFT JOIN 
    Sections t3 ON t1.Section = t3.section_no 
LEFT JOIN 
    Groups t4 on t1.group = t4.group 
WHERE 
    (desc + ':' + Item_code + ':' + certify_code) LIKE '%keyword1%' 
AND (desc + ':' + Item_code + ':' + certify_code) LIKE '%keyword2%' 
于 2015-06-11T08:47:52.383 回答