我想弄清楚我当前的时间是否介于两个给定时间之间。它工作正常,除非我的结束时间是在午夜之后,而我的开始时间是白天。
例如,如果我有:
开始: 17:00
结束时间: 03:00
当前时间: 18:30
然后它就不会显示了。
我确实理解逻辑哪里出了问题,但我无法从逻辑上和概念上找到解决方法。我的意思是,您可以声称 06:30 PM 确实介于 05 PM 和 03 AM 之间,但然后是第二天的 03 AM!
我们如何克服这个问题?
下面是我的查询:
SELECT * FROM products
JOIN restaurants
ON (products.Venue = restaurants.name)
WHERE products.Drink_Category = Beer
AND restaurants.Area = Racks
AND (
HOUR(18:30) >= HOUR(products.Start) -- here is where the time is set
AND HOUR(18:30) <= HOUR(products.End) --and here is the end
)
ORDER BY products.Price/products.Multiple ASC;
我也尝试过 BETWEEN
查询,但它不适用于那些特定情况。
最佳答案
基本上,当 products.End
小于 products.Start
时,只需添加 24 以考虑换行。例如
SELECT * FROM products
JOIN restaurants
ON (products.Venue = restaurants.name)
WHERE products.Drink_Category = Beer
AND restaurants.Area = Racks
AND (
HOUR(18:30) >= HOUR(products.Start) -- here is where the time is set
AND HOUR(18:30) <= CASE
WHEN products.End<products.Start
THEN HOUR(products.End)+24
ELSE HOUR(products.End) END --and here is the end
)
ORDER BY products.Price/products.Multiple ASC;
关于mysql - 使用 MySQL 的时差 - 午夜后的结束时间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062374/