9

我有一个查询,它根据一组条件收集信息。基本上我想知道一个位置是否在当天支付了超过 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。

4

7 回答 7

7

您需要将您的 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' + '%')
    )
于 2012-07-23T17:51:16.310 回答
5

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

WHERE 
(
    (1 = 1)
    AND
    (2 = 2)
)
OR
( 3 = 3 )
于 2012-07-23T17:55:15.967 回答
4

看起来您缺少一组括号:

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 检查

于 2012-07-23T17:51:16.593 回答
3

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' + '%') )
于 2012-07-23T17:50:32.487 回答
3
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' + '%')

我结合了您的一些建议,并得出了上述建议。这行得通。谢谢!

于 2012-07-23T17:55:55.277 回答
2

尝试以下查询:

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' + '%')
于 2012-07-23T17:50:47.693 回答
1

您需要将 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' + '%')
于 2012-07-23T17:50:39.040 回答