如何比较sql中where子句中的多个日期

时间:2016-07-30 06:58:37

标签: sql date where

 select truckid 
 from truck_log 
 where '20160804' between ruck_log.pickupdate and truck_log.ETA  
   and '20160806' between truck_log.pickupdate and truck_log.ETA

如何比较where子句中的两个日期,当我这样做时,查询只接受第一次比较?

2 个答案:

答案 0 :(得分:0)

ruck_log是拼写错误,应该是Truck_log吗?

如果是,那么

select truckid 
 from truck_log 
 where '20160804' between truck_log.pickupdate and truck_log.ETA  
   and '20160806' between truck_log.pickupdate and truck_log.ETA

将选择在pickupdate和ETA之间有所需日期的所有行

          <-- 20160804   20160806 -->
pickupdate                           ETA  

如果您只需要选择一个OR秒,请使用OR子句而不是AND。

答案 1 :(得分:-1)

在where子句中添加“OR”而不是“AND”:

查询:

select truckid from truck_log 

where  ('20160804' between truck_log.pickupdate and truck_log.ETA) 

      or ('20160806' between truck_log.pickupdate and truck_log.ETA)