这是我的 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)
)
上面的查询返回表的所有 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)
我期待顶部的查询返回 0 行,因为我使用了 AND 操作数,所以即使日期比较返回行,组合查询也不应该返回任何内容,因为时间比较返回 false。
创建表的脚本:
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