如何在开始日期时间和结束日期时间之间编写查询以查询数据

时间:2017-04-08 07:04:13

标签: mysql

大家好我要查询一下,在数据库中查看一些start_datetime和end_datetime假设会有一个名为test的表,列是className,startDateTime和endDateTime

示例数据classname =" I" startDateTime =' 2017-04-18 08:00:00'和endDateTime =' 2017-04-18 09:00:00'

我的查询就好像我的价值如下,它不应该获取示例数据

startDateTime = 2017-04-18 07:00:00""和endDateTime =" 2017-04-18 07:55:00"

startDateTime = 2017-04-18 10:00:00""和endDateTime =" 2017-04-18 11:00:00"

startDateTime = 2017-04-19 08:00:00""和endDateTime =" 2017-04-19 09:00:00"

它应该获取示例数据

startDateTime = 2017-04-18 08:00:00""和endDateTime =" 2017-04-18 09:00:00"

startDateTime = 2017-04-18 08:10:00""和endDateTime =" 2017-04-18 08:55:00"

startDateTime = 2017-04-18 08:30:00""和endDateTime =" 2017-04-18 09:30:00"

 SELECT * FROM timetable_tbl  WHERE className='I' and 
    ((StartDate>='startDateTimeValue' and StartDate<'startDateTimeValue') or
    (StartDate<='startDateTimeValue' and StartDate<'startDateTimeValue') or
    (StartDate>='startDateTimeValue' and EndDate<='endDateTimeValue'));

1 个答案:

答案 0 :(得分:1)

以下逻辑满足我的所有上述标准

((startDate>? And endDate<? ) or (startDate>=? And endDate<=? ) or (startDate<? And endDate>? ) or (startDate<? And endDate>? ))