我有一个查询试图根据日期过滤信息,但它不是很有效。
使用此查询:
select CONVERT(VARCHAR, r.ondate, 111)
from WS_ACTIVITY r
我得到了这个结果:
------------------------------
2017/03/16
但是在运行此查询时:
SELECT r.*
FROM [dbo].[WS_ACTIVITY] r
WHERE r.request_type = 2 OR r.request_type = 7 OR r.request_type = 51 AND
(CONVERT(VARCHAR, r.ondate, 111)
BETWEEN '2017/10/18' AND '2017/10/18') ORDER BY r.user_id
我得到以下结果:
id egn_bul request_xml response_xml user_id user_name user_department_id user_department_name request_id request_type is_from_cashe ondate ondate_response
----------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- ---------------------------------------------------------------------------------------------------- ------------------ ---------------------------------------------------------------------------------------------------- ----------- ------------ ------------- ----------------------- -----------------------
7 8903304365 NULL NULL NULL NULL NULL NULL 7 1 2017-03-16 00:00:00.000 NULL
当我不应该,我错过了什么?
答案 0 :(得分:1)
试试这个
(r.request_type = 2 OR r.request_type = 7 OR r.request_type = 51)
AND (CONVERT(VARCHAR, r.ondate, 111) BETWEEN '2017/10/18' AND '2017/10/18')
因此,两个条款都应该为true才能返回结果。这与Operator Precedence
有关答案 1 :(得分:1)
代码应如下所示:
SELECT r.*
FROM [dbo].[WS_ACTIVITY] r
WHERE (r.request_type = 2 OR r.request_type = 7 OR r.request_type = 51 ) AND (CONVERT(VARCHAR, r.ondate, 111) BETWEEN '2017/10/18' AND '2017/10/18') ORDER BY r.user_id
在代码中,已过帐的条件r.request_type = 7
为TRUE,因此您的数据之间没有区别,因为您的条件之间有OR