我有一个基于一组条件收集信息的查询。基本上我想知道某个地点当天支付的金额是否超过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.
答案 0 :(得分: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' + '%')
)
答案 1 :(得分:5)
这实际上取决于您使用OR的内容。您的Paid_Out_Amount大于50或Paid_Out_Amount类似于N%Filter%。在你的子句
中添加一些括号WHERE
(
(1 = 1)
AND
(2 = 2)
)
OR
( 3 = 3 )
答案 2 :(得分: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检查
答案 3 :(得分: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' + '%') )
答案 4 :(得分: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' + '%')
我提出了一些你的建议,并提出了上述建议。这有效。谢谢!
答案 5 :(得分: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' + '%')
答案 6 :(得分:1)
你需要在paranthesis中包含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' + '%')