sql - 如何在“ Where”子句中使用“ and”和“ or”

Translate

我有一个查询,它基于一组条件来收集信息。基本上,我想知道某个地点当天的付款金额是否超过$ 50,或者评论部分中是否包含“过滤器”一词...

我的查询是:

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Amount > 50) OR
                         (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')

问题是它返回460个结果,并且应该仅返回2。

This question and all comments follow the "Attribution Required."

所有的回答

Translate

您需要将Paid_Out_Amoutn和Paid_Out_Comment条件包装在第二组括号中:

SELECT Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, 
    Update_UserName, Till_Number 
FROM Paid_Out_Tb 
WHERE (Store_Id = 1929) AND (Paid_Out_Datetime >= 
    DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND 
    (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) AND 
    (
        (Paid_Out_Amount > 50) OR (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')
    )
来源
Translate

这实际上取决于您使用OR的目的。您的Paid_Out_Amount大于50或Paid_Out_Amount类似于N%Filter%。在您的条款中添加一些括号

WHERE 
(
    (1 = 1)
    AND
    (2 = 2)
)
OR
( 3 = 3 )
来源
Translate

您似乎缺少了一组括号:

SELECT        
    Store_Id
    , Paid_Out_Amount
    , Paid_Out_Comment
    , Paid_Out_Datetime
    , Update_UserName
    , Till_Number
FROM            
    Paid_Out_Tb
WHERE        
    Store_Id = 1929
    AND Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)
    AND Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
    AND
    (
        Paid_Out_Amount > 50
        OR
        LOWER(Paid_Out_Comment) LIKE '%filter%'
    )

您还需要查看该LIKE命令-更新代码以将所有注释设置为小写并搜索单词过滤器。

还要检查命令BETWEEN进行Paid_Out_Datetime检查

来源
Translate

AND优先于OR。如果要控制优先级,则需要对条件进行分组。尝试这个:

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                     GETDATE()), 0)) AND ( (Paid_Out_Amount > 50) OR
                     (Paid_Out_Comment LIKE N'%' + 'Filter' + '%') )
来源
Translate
SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE        (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Amount > 20) OR
                         (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')

我结合了您的大量建议,并提出了上述建议。这有效。谢谢!

来源
Translate

尝试以下查询:

SELECT        Store_Id, Paid_Out_Amount, Paid_Out_Comment, Paid_Out_Datetime, Update_UserName, Till_Number
FROM            Paid_Out_Tb
WHERE       ((Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Amount > 50)) OR
                         (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')
来源
Translate

您需要用括号将where子句的第一部分(最多为“ OR”)括起来。

(
 (Store_Id = 1929) AND (Paid_Out_Datetime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()) - 1, 0)) AND (Paid_Out_Datetime < DATEADD(day, DATEDIFF(day, 0, 
                         GETDATE()), 0)) AND (Paid_Out_Amount > 50)

) OR (Paid_Out_Comment LIKE N'%' + 'Filter' + '%')
来源
Leave a Reply
You must be logged in to post a answer.
关于作者