OR子句影响AND子句?

时间:2015-06-10 22:41:10

标签: sql sql-server select or-operator and-operator

我正在创建一个SQL语句,用于在数据库中搜索特定列中的关键字。我需要查询来返回符合keyword1 AND keyword2标准的记录。这很好用,但是我需要允许从多个列中查询关键字。添加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%'

5 个答案:

答案 0 :(得分:3)

void print_Matrix(){ unsigned int i, j; for (i = 0; i <= 9; i++){ // ^^^ Use 0 instead of 1 for (j = 0; j <= 9; j++) // ^^^ Use 0 instead of 1 printf("%u ", Matrix[i][j]); printf("\n"); } } 的优先级高于AND。如果你想创建一个逻辑上说&#34;条件匹配1在这些列中的任何一列上匹配而且keywrod2匹配任何这些列的条件,那么你需要围绕每个OR。带括号的参数,以避免它优先:

(顺便说一下,使用AND可以简化选择列表中的描述说明)

coalesce

答案 1 :(得分:1)

试试这个:

SELECT  b.*
    FROM  
      ( SELECT  last_name, first_name
            FROM  contacts
            WHERE  client_id = 22844
              AND  id = 717604 
      ) AS a
    JOIN  contacts AS b
    WHERE  deleted_at IS NULL
      AND    b.last_name >= a.last_name
      AND  ( b.first_name > a.first_name
         OR  b.last_name > a.last_name 
           )
    ORDER BY  b.last_name, b.first_name
    LIMIT  1; 
-- and have these indexes on contacts:
INDEX(client_id, id),
INDEX(last_name, first_name) 

我清理了你的case语句以使用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%') 代替(ISNULL也可以),并在你的逻辑位置添加了括号。

答案 2 :(得分:1)

通过使用rewrite ^/forums/(.*)$ /forums.php?topic_id=$1 last; rewrite ^/forums/(.*)$ /forums.php?category_id=$1 last;

括号将WHERE部分改为如下所示
()

答案 3 :(得分: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%'
      )

答案 4 :(得分: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%'