php - 通过mysql获取时间间隔的数据

标签 php mysql sql database

我需要获取当前时间小于 2 小时和大于当前时间 2 小时的数据。在 DB 表中,我创建了 2 个字段,一个是开始时间,另一个是关闭时间。这两个字段都是 VARCHAR 格式。在我的数据库表下方。

http://i.imgur.com/JJK5c21.png

我已经创建了 mysql 查询,但这不起作用。

select * 
  from table_name 
 where TIME_FORMAT('states','h:i %A') > '$curr-2 ' 
   and TIME_FORMAT('close_time','h:i %A') < '$curr+2'

$curr-2 = Current time - 2 Hours (minus) 
$curr+2 = Current time + 2 Hours (Plus)

此 SQL 不起作用。

最佳答案

如果字段是datetimetime,那肯定会更有意义吗?但是,以下内容可能会满足您的需求...

select * from `table_name`
   where 
   time_format( time( now() ),'%h:%i') 
   between 
     time( concat( time_format( `states`,'%h')+2,':',time_format( `states`,'%i') ) ) 
   and 
     time( concat( time_format( `close_time`,'%h')+2,':',time_format( `close_time`,'%i') ) )

关于php - 通过mysql获取时间间隔的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184124/

相关文章:

mysql - 为什么在我的 MySQL 服务器上得到 'Binary logging not possible.'?

MySQL 仅更新 WHERE max()

php - 更新 mysql 中的行

php - Laravel 4 - 如何在每次访问时对字段进行 json_decode?

java - 使用ScrollableResults和MySql的字符编码问题

sql - Oracle 数据库创建目录

c# - 为 Localhost c# 使用 Oledb 连接字符串

php - Laravel 处理异常

php - 不会出现的错误

MySQL——删除或索引?