使用 Date_Format MySQL 获取最短时间
Get Mearest Time Using Date_Format MySQL
到目前为止,这是我的代码
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and ending_time >= starting_time
and day = DATE_FORMAT(Now(),'%W')"
starting_time ending_time day
07:00:00 08:30:00 Tuesday
08:30:00 10:00:00 Tuesday
它只是returns数据库中的第一条记录。我想要的是,如果现在的时间是 8:30AM,那么下一个时间表将显示为 8:30AM - 10:00AM。我如何使用上面的代码来做到这一点?或者还有其他方法可以实现吗?
示例架构
您想添加 "I want the starting time to be less-than-or-equal to now" 逻辑,可以通过 starting_time<=DATE_FORMAT(NOW(),'%T')
完成:
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and starting_time<=DATE_FORMAT(NOW(),'%T')
and day = DATE_FORMAT(Now(),'%W')"
请注意,如评论中所述,ending_time >= starting_time
是多余的,因为它已被 starting_time <= ending_time
满足。
到目前为止,这是我的代码
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and ending_time >= starting_time
and day = DATE_FORMAT(Now(),'%W')"
starting_time ending_time day
07:00:00 08:30:00 Tuesday
08:30:00 10:00:00 Tuesday
它只是returns数据库中的第一条记录。我想要的是,如果现在的时间是 8:30AM,那么下一个时间表将显示为 8:30AM - 10:00AM。我如何使用上面的代码来做到这一点?或者还有其他方法可以实现吗?
示例架构
您想添加 "I want the starting time to be less-than-or-equal to now" 逻辑,可以通过 starting_time<=DATE_FORMAT(NOW(),'%T')
完成:
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and starting_time<=DATE_FORMAT(NOW(),'%T')
and day = DATE_FORMAT(Now(),'%W')"
请注意,如评论中所述,ending_time >= starting_time
是多余的,因为它已被 starting_time <= ending_time
满足。