mysql - 使用 MySQL 的时差 - 午夜后的结束时间不起作用

标签 mysql sql datetime

我想弄清楚我当前的时间是否介于两个给定时间之间。它工作正常,除非我的结束时间是在午夜之后,而我的开始时间是白天。

例如,如果我有:

开始: 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/

相关文章:

mysql - 如何使用多个最昂贵的项目在 sql 中查找最昂贵的项目?

mysql - 尝试将一个值插入一列的一行时出现 SQL 错误

javascript - 在 Javascript 中验证 var 是否是有效的日期时间对象

php - 如何在PHP中计算总记录并重复主div 3次

MySQL - 如何返回不同的 ID,其中同一 ID 的所有行都具有空字段值

sql - 在单个单元格中选择结果

sql - SQL注入(inject)攻击后更新所有SQL Server列

java - 从 PHP 到 Java 的日期时间 (Android) : how to convert a string to a DateTime object?

php - 想要将 strtotime 值与当前时间进行比较并输出 : something x min ago

php - 多个 INSERT 查询获取每个的last_insert_id