[英]Select query involving comparison between dates and time
Here is my MS Sql table.. Table: http://snag.gy/L6ag1.jpg 这是我的MS Sql表。.表: http : //snag.gy/L6ag1.jpg
SELECT
*
FROM Table1
WHERE
Class = '12'
AND Section = 'B'
AND '3/4/2013' BETWEEN DATE AND TODATE
and '3/14/2013' between Date and ToDate
or '3/4/2013' < Date and '3/14/2013' > ToDate
AND (
'12:00 PM' between [StartTime] and endtime
and '1:00 PM' between [StartTime] and endtime
or ('12:00 PM' < StartTime and '1:00 PM' > EndTIme)
)
The above query is returning all the 6 rows of the table while the below query returns 0 rows.. 上面的查询返回表的所有6行,而下面的查询返回0行。
SELECT
*
FROM Teacher
WHERE
Class = '12'
AND Section = 'B'
and '12:00 PM' between [StartTime] and endtime
and '1:00 PM' between [StartTime] and endtime
or ('12:00 PM' < StartTime and '1:00 PM' > EndTIme)
I was expecting the query at the top to return 0 rows since i used AND operand, so even if the date comparison returns rows the combined query shouldnt return anything since time comparison returns false. 自从我使用AND操作数以来,我一直期望最上面的查询返回0行,所以即使日期比较返回行,由于时间比较返回false,组合查询也不应该返回任何内容。
SCRIPT TO CREATE TABLE: 编写表的脚本:
INSERT INTO [SLIITComDB].[dbo].[Teacher]
([TeacherId]
,[TeacherName]
,[Class]
,[Section]
,[SubjectId]
,[Date]
,[ToDate]
,[Day]
,[StartTime]
,[EndTIme]
,[Absent])
VALUES
(<TeacherId, int,>
,<TeacherName, varchar(50),>
,<Class, varchar(50),>
,<Section, varchar(50),>
,<SubjectId, varchar(50),>
,<Date, date,>
,<ToDate, date,>
,<Day, varchar(50),>
,<StartTime, time(7),>
,<EndTIme, time(7),>
,<Absent, varchar,>)
GO
In SQL, AND
operator has precedence over OR
operator. 在SQL中,
AND
运算符的优先级高于OR
运算符。
So in your first query, the conditions look like this: 因此,在您的第一个查询中,条件如下所示:
WHERE
( Class = '12'
AND Section = 'B'
AND '3/4/2013' BETWEEN DATE AND TODATE
and '3/14/2013' between Date and ToDate
) OR (
'3/4/2013' < Date and '3/14/2013' > ToDate
AND (
'12:00 PM' between [StartTime] and endtime
and '1:00 PM' between [StartTime] and endtime
or ('12:00 PM' < StartTime and '1:00 PM' > EndTIme)
))
The result is TRUE
when either of the top or bottom sections (separated by OR
) are TRUE
. 当顶部或底部部分(由
OR
分隔)为TRUE
时,结果为TRUE
。
The conditions in your second query also look like: 第二个查询中的条件也如下所示:
WHERE
( Class = '12'
AND Section = 'B'
and '12:00 PM' between [StartTime] and endtime
and '1:00 PM' between [StartTime] and endtime
)
or ('12:00 PM' < StartTime and '1:00 PM' > EndTIme)
You first query can return result even if time comparisions are all FALSE
because your first two date comparisions can be TRUE
. 即使时间比较都为
FALSE
,您的第一个查询也可以返回结果,因为前两个日期比较可以为TRUE
。
Actually it is your OR which causes this: 实际上,是由您的OR导致的:
The first part before the or is true according to your data. 或的第一部分根据您的数据为真。 So these will be returned.
因此这些将被退回。 Add brackets to clarify things.
添加方括号以澄清问题。
True: 真正:
Class = '12'
AND Section = 'B'
AND '3/4/2013' BETWEEN DATE AND TODATE
and '3/14/2013' between Date and ToDate
OR
False: 假:
'3/4/2013' < Date and '3/14/2013' > ToDate
AND (
'12:00 PM' between [StartTime] and endtime
and '1:00 PM' between [StartTime] and endtime
or ('12:00 PM' < StartTime and '1:00 PM' > EndTIme)
)
Try this simplified : 试试这个简化:
select *
from Table1
where 1 = 1
or 2 = 2
and 1 = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.