SQL中的括号规则。似乎没有用

时间:2019-02-05 16:13:31

标签: sql parentheses

SQL的新手。 “ Like'%”和“ not like'%”语句返回错误值时出现问题。我认为是括号。当我使用以下内容时,即使我选择了它们,我仍然会看到“ YMCA”和“ United Way”。我尝试将单个括号嵌套到一个大括号中,并尝试将单个括号放在每个语句周围。什么都没用。这是代码。任何建议深表感谢!

SELECT TRANSACTION_DATE, MERCHANT_NAME 
FROM CardTrans 
WHERE 
   (
     (TRANSACTION_DATE BETWEEN '2018-03-01' AND  '2018-03-15') 
     OR 
     (    TRANSACTION_DATE IS NULL 
      AND POST_DATE BETWEEN '2018-03-01' AND '2018-03-15')
   ) 
AND (TRANSACTION_BASE_TYPE = 'debit') 
and  (DESCRIPTION LIKE '%SWAIR%') 
OR   (DESCRIPTION LIKE '%SW AIR%') 
OR   (DESCRIPTION LIKE '%SWA INFL%') 
OR   (DESCRIPTION LIKE '%*Southwest Air%') 
AND   DESCRIPTION NOT LIKE '%AMAZON%' 
AND   DESCRIPTION NOT LIKE '%AMZ%' 
AND   DESCRIPTION NOT LIKE '%YMCA OF SOUTHWES%' 
AND   DESCRIPTION NOT LIKE '%UNITED WAY OF SOUTHWES%' 
ORDER BY DESCRIPTION;
GO   

3 个答案:

答案 0 :(得分:3)

我猜你想要:

List<int>

您的问题是了解fooWHERE ( (TRANSACTION_DATE BETWEEN '2018-03-01' AND '2018-03-15') OR (TRANSACTION_DATE IS NULL AND POST_DATE BETWEEN '2018-03-01' AND '2018-03-15') ) AND TRANSACTION_BASE_TYPE = 'debit' AND (DESCRIPTION LIKE '%SWAIR%' OR DESCRIPTION LIKE '%SW AIR%' OR DESCRIPTION LIKE '%SWA INFL%' OR DESCRIPTION LIKE '%*Southwest Air%' ) AND DESCRIPTION NOT LIKE '%AMAZON%' AND DESCRIPTION NOT LIKE '%AMZ%' AND DESCRIPTION NOT LIKE '%YMCA OF SOUTHWES%' AND DESCRIPTION NOT LIKE '%UNITED WAY OF SOUTHWES%' 的优先级规则。 AND具有更高的优先级,因此:

OR

解析为:

AND

您可能会在结果中看到 DESCRIPTION LIKE '%SWA INFL%' OR DESCRIPTION LIKE '%*Southwest Air%' AND DESCRIPTION NOT LIKE '%AMAZON%'

答案 1 :(得分:0)

您有一系列AND,它们确实排除了'%YMCA OF SOUTHWES%''%UNITED WAY OF SOUTHWES%'
但您也有以下OR

    ...  OR
   (DESCRIPTION LIKE '%SW AIR%') OR
   (DESCRIPTION LIKE '%SWA INFL%') OR 
   (DESCRIPTION LIKE '%*Southwest Air%')

如果DESCRIPTION包含排除的项目,则可以让它们退回。

答案 2 :(得分:-1)

这是您的查询,其中删除了所有不需要的括号

SELECT TRANSACTION_DATE, MERCHANT_NAME 
FROM CardTrans 
WHERE (
 TRANSACTION_DATE BETWEEN '2018-03-01' AND  '2018-03-15'           
    OR 
  TRANSACTION_DATE IS NULL 
  AND POST_DATE BETWEEN '2018-03-01' AND '2018-03-15'
       )     
AND TRANSACTION_BASE_TYPE = 'debit'
and DESCRIPTION LIKE '%SWAIR%'
OR DESCRIPTION LIKE '%SW AIR%' 
OR DESCRIPTION LIKE '%SWA INFL%'
OR DESCRIPTION LIKE '%*Southwest Air%'
AND DESCRIPTION NOT LIKE '%AMAZON%' 
AND DESCRIPTION NOT LIKE '%AMZ%' 
AND DESCRIPTION NOT LIKE '%YMCA OF SOUTHWES%' 
AND DESCRIPTION NOT LIKE '%UNITED WAY OF SOUTHWES%' 
ORDER BY DESCRIPTION; GO 

记住AND是在OR之前完成的,所以1 OR 2 AND 2为TRUE->(2AND2)OR1