需求:找出时间A到B时间段是否已经请过假了,或者这个期间已经请过假了

select count(id) from tblName where ...

and ( (cast(concat(date_from,' ',time_from) as timestamp)    > '2018-03-30 08:00' 

  and cast(concat(date_from,' ',time_from) as timestamp)   < '2018-03-30 12:00')
               or( cast(concat(date_from,' ',time_from) as timestamp)   < '2018-03-30 08:00' 
                    and cast(concat(date_to,' ',time_to) as timestamp)  > '2018-03-30 08:00')
  or (cast(concat(date_from,' ',time_from) as timestamp)  = '2018-03-30 08:00' 
                     and cast(concat(date_from,' ',time_from) as timestamp)  <= '2018-03-30 12:00')
  )

有,则表示在这个时间段里有数据

解释:

找开始时间A到结束时间B的区间,是不是已经在数据库中有了

  1. 开始时间小于A,并且结束时间大于A;
  2. 开始时间小于B,并且结束时间大于等于B;
  3. 开始时间大于A,并且结束时间小于B;
  4. 开始时间等于A,并且结束时间大于等于B。

见下面抽象图解 -_-||:

SQL判断两个日期时间区间是否有相交

相关文章: