检查给定时间是否在两次(小时,分钟)格式之间

时间:2013-11-27 19:39:34

标签: mysql sql

我在MySQL中有接下来的6个字段。

TIME_RIGHT_NOW_HOUR (INT)
TIME_RIGHT_NOW_MINUTE (INT)
TIME_WORKING_FROM_HOUR (INT)
TIME_WORKING_FROM_MINUTE (INT)
TIME_WORKING_TO_HOUR (INT)
TIME_WORKING_TO_MINUTE (INT)

小时和分钟为24时格式。

我需要检查两次工作时间之间的时间。 例如,如果TIME_RIGHT_NOW_HOUR是15并且TIME_RIGHT_NOW_MINUTE是30 - > 15:30 是在TIME_WORKING_FROM_HOUR 10,TIME_WORKING_FROM_MINUTE 15之间 10:15和TIME_WORKING_TO_HOUR 18,TIME_WORKING_TO_MINUTE 20 - > 18:20

4 个答案:

答案 0 :(得分:2)

也许maketime()正是你所寻找的。

SELECT 1
FROM table
WHERE 
  MAKETIME(TIME_RIGHT_NOW_HOUR,TIME_RIGHT_NOW_MINUTE,0)
    BETWEEN MAKETIME(TIME_WORKING_FROM_HOUR,TIME_WORKING_FROM_MINUTE,0)
    AND MAKETIME(TIME_WORKING_TO_HOUR,TIME_WORKING_TO_MINUTE,0);

答案 1 :(得分:1)

非常简单的解决方案:将小时和时间转换为浮动然后比较:

    TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100 -----> 10.15
    TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100; ------> 18.20
    TIME_RIGHT_NOW_HOUR + TIME_RIGHT_NOW_MINUTE / 100; ------> 15.30

SQL WHERE:

    WHERE ( (TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100) <= (TIME_RIGHT_NOW_HOUR +   TIME_RIGHT_NOW_MINUTE / 100) )
    AND ( (TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100) >= (TIME_RIGHT_NOW_HOUR +     TIME_RIGHT_NOW_MINUTE / 100) )

答案 2 :(得分:1)

这适用于5:00 - &gt; 23:00和23:00 - &gt; 5:00(下一天)

    WHERE
    (
            -- CASE: 5:00 -> 23:00
            ( 
                    TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100.00 < TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100.00
            )
            AND
            (
                    (
                            TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100.00 <= TIME_RIGHT_NOW_HOUR + TIME_RIGHT_NOW_MINUTE / 100.00
                    )
                    AND 
                    (
                            TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100.00 >= TIME_RIGHT_NOW_HOUR + TIME_RIGHT_NOW_MINUTE / 100.00
                    )
            )
    )
    OR
    (
            -- CASE: 23:00 -> 5:00 of Next day
            ( 
                    TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100.00 >= TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100.00
            )
            AND
            (
                (
                    TIME_RIGHT_NOW_HOUR + TIME_RIGHT_NOW_MINUTE / 100.00 >= TIME_WORKING_FROM_HOUR + TIME_WORKING_FROM_MINUTE / 100.00
                )
                OR
                (
                    TIME_RIGHT_NOW_HOUR + TIME_RIGHT_NOW_MINUTE / 100.00 <= TIME_WORKING_TO_HOUR + TIME_WORKING_TO_MINUTE / 100.00
                )
            )
    )

答案 3 :(得分:1)

您可以使用XOR(或IF功能),以应对午夜滚动(即,FROM是22:00,TO是06:00,问题是“这个时期内外23:30 ?“,在数学上是在外面,但在时钟方面是在里面。”

SELECT 
    ( TIME_WORKING_FROM_HOUR*60+TIME_WORKING_FROM_MINUTE < 
    TIME_WORKING_TO_HOUR*60+TIME_WORKING_TO_MINUTE )
    XOR NOT
    (
    TIME_RIGHT_NOW_HOUR*60+TIME_RIGHT_NOW_MINUTE BETWEEN
        TIME_WORKING_FROM_HOUR*60+TIME_WORKING_FROM_MINUTE
        AND
        TIME_WORKING_TO_HOUR*60+TIME_WORKING_TO_MINUTE
    )

测试

CREATE TABLE test ( TIME_RIGHT_NOW_HOUR integer, TIME_RIGHT_NOW_MINUTE integer, 
TIME_WORKING_FROM_HOUR integer, TIME_WORKING_FROM_MINUTE integer,
TIME_WORKING_TO_HOUR integer, TIME_WORKING_TO_MINUTE integer );

INSERT INTO test VALUES 
    ( 7, 30, 8, 00, 17, 00 ), 
    ( 12, 30, 8, 00, 17, 00 ),
    ( 4, 30, 22, 00, 06, 00 ),
    ( 0, 0, 22, 00, 06, 00 ),
    ( 22, 0, 6, 0, 6, 5 );

SELECT  TIME_WORKING_FROM_HOUR AS FROM_H,
    TIME_WORKING_FROM_MINUTE AS FROM_M,
    TIME_WORKING_TO_HOUR AS TO_H,
    TIME_WORKING_TO_MINUTE AS TO_M,
    TIME_RIGHT_NOW_HOUR AS NOW_H,
    TIME_RIGHT_NOW_MINUTE AS NOW_M,
    ( TIME_WORKING_FROM_HOUR*60+TIME_WORKING_FROM_MINUTE < 
    TIME_WORKING_TO_HOUR*60+TIME_WORKING_TO_MINUTE )
    XOR NOT
    (
    TIME_RIGHT_NOW_HOUR*60+TIME_RIGHT_NOW_MINUTE BETWEEN
        TIME_WORKING_FROM_HOUR*60+TIME_WORKING_FROM_MINUTE
        AND
        TIME_WORKING_TO_HOUR*60+TIME_WORKING_TO_MINUTE
    ) AS in_orario FROM test;

结果:

+--------+--------+------+------+-------+-------+-----------+
| FROM_H | FROM_M | TO_H | TO_M | NOW_H | NOW_M | in_orario |
+--------+--------+------+------+-------+-------+-----------+
|      8 |      0 |   17 |    0 |     7 |    30 |         0 |
|      8 |      0 |   17 |    0 |    12 |    30 |         1 |
|     22 |      0 |    6 |    0 |     4 |    30 |         1 |
|     22 |      0 |    6 |    0 |     0 |     0 |         1 |
|      6 |      0 |    6 |    5 |    22 |     0 |         0 |
+--------+--------+------+------+-------+-------+-----------+
5 rows in set (0.00 sec)

this SQLfiddle中,条件已移至WHERE