mysql如何查询当前时间是否在某个时间段内

MySql 码拜 9年前 (2015-09-27) 9516次浏览
 

数据结构如下:

id     starttime       endtime

1 20:28:42 12:20:48

2 20:00:00 22:00:00

3 11:00:00 10:00:00

………..

starttime和endtime为time类型

如何查询出当前时间在(starttime , endtime)范围内的数据

#3
没太理解楼主说的当前时间在(starttime , endtime)范围内的数据,想要的是下面的结果集吗

select *

from table

where starttime<=curtime()

and endtime>=curtime();

#4
select *

from 数据结构如下

where now() between starttime and endtime

#5
你给的数据就没有范围,有些是starttime 大于 endtime有些是starttime 小于 endtime,这个根本不是范围值
#6

40分

开始时间小于结束时间 (时间段在一天内)

开始时间大于结束时间 (时间段跨天<不在同一天>)

select id, case when starttime<=endtime then 1 else 0 end as s1

from table

where  (s1=1)  and  curtime() >= starttime and curtime() <= endtime ;

untion all

select id, case when starttime<=endtime then 1 else 0 end as s1

from table

where  (s1=0)  and  curtime() <= starttime and curtime() >= endtime ;

#7

回复6楼:

这是我想出来的答案:

select from table

 where (starttime<endtime and curtime() between starttime and endtime) 

or          (starttime>endtime and curtime()>starttime and curtime<endtime )

#8

回复7楼:

这个写错了,是这样

where (starttime<endtime and curtime() between starttime and endtime) 

or         (starttime>endtime and curtime()>endtime and curtime<starttime)

#9

回复8楼:

别用between 了,  直接用判断就OK了.

where (starttime<=endtime and curtime()<=endtime and curtime>=starttime) 

or         (starttime>endtime and curtime()>endtime and curtime<starttime)

#10
select * from tab1

where starttime>=date(“”2014-06-01″”) AND starttime>=time(“”10:00:00″”)

and endtime<=date(“”2014-06-01″”) AND endtime<=time(“”23:59:59″”);


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明mysql如何查询当前时间是否在某个时间段内
喜欢 (1)
[1034331897@qq.com]
分享 (0)