MySQL数据库的间隔时间

时间:2019-06-14 09:41:15

标签: mysql time intervals

我需要在06:00 AM到08:00 AM之间从MySQL数据库表中获取所有行。

由于我不理解输出,因此我启动了此sql查询,但未成功。

时间间隔(06:00 AM和08:00 AM)不正确,如何解决呢?

在此先感谢您的帮助。

mysql> SELECT
    DAYOFWEEK(dStart) AS DOW,
    dStart,
    dEnd,
    cd
FROM
    `2019tblCd`
WHERE
    (
        cd IN ('P')
        AND DAYOFWEEK(dStart) BETWEEN 2
        AND 6
        AND (
            time(dStart) >= '06:00:00'
            OR time(dEnd) <= '08:00:00'
        )
    )
ORDER BY
    STR_TO_DATE(dStart, '%Y-%m-%d %H:%i:%s') DESC
LIMIT 10;

+-----+---------------------+---------------------+----+
| DOW | dStart              | dEnd                | cd |
+-----+---------------------+---------------------+----+
|   6 | 2019-06-07 19:15:41 | 2019-06-07 19:38:53 | P  |
|   6 | 2019-06-07 18:51:28 | 2019-06-07 19:17:47 | P  |
|   6 | 2019-06-07 18:29:39 | 2019-06-07 18:35:32 | P  |
|   6 | 2019-06-07 18:22:27 | 2019-06-07 18:45:33 | P  |
|   6 | 2019-06-07 18:09:07 | 2019-06-07 18:38:51 | P  |
|   6 | 2019-06-07 18:05:31 | 2019-06-07 18:42:46 | P  |
|   6 | 2019-06-07 17:48:01 | 2019-06-07 17:58:08 | P  |
|   6 | 2019-06-07 17:34:26 | 2019-06-07 18:23:23 | P  |
|   6 | 2019-06-07 17:26:54 | 2019-06-07 18:08:29 | P  |
|   6 | 2019-06-07 17:10:28 | 2019-06-07 17:48:48 | P  |
+-----+---------------------+---------------------+----+
10 rows in set

编辑#2

+-----+---------------------+---------------------+----+
| DOW | dStart              | dEnd                | cd |
+-----+---------------------+---------------------+----+
|   6 | 2019-06-07 07:15:41 | 2019-06-07 07:38:53 | P  |
|   6 | 2019-06-07 06:51:28 | 2019-06-07 07:17:47 | P  |
|   6 | 2019-06-07 06:29:39 | 2019-06-07 06:35:32 | P  |
|   6 | 2019-06-07 06:22:27 | 2019-06-07 06:45:33 | P  |
|   6 | 2019-06-07 06:09:07 | 2019-06-07 06:38:51 | P  |
|   6 | 2019-06-07 06:05:31 | 2019-06-07 06:42:46 | P  |
|   6 | 2019-06-07 05:48:01 | 2019-06-07 05:58:08 | P  |
|   6 | 2019-06-07 05:34:26 | 2019-06-07 06:23:23 | P  |
|   6 | 2019-06-07 05:26:54 | 2019-06-07 06:08:29 | P  |
|   6 | 2019-06-07 05:10:28 | 2019-06-07 05:48:48 | P  |
+-----+---------------------+---------------------+----+
10 rows in set

编辑#1

正确的输出是:

+-----+---------------------+---------------------+----+
| DOW | dStart              | dEnd                | cd |
+-----+---------------------+---------------------+----+
|   6 | 2019-06-07 06:15:41 | 2019-06-07 07:38:53 | P  |
|   6 | 2019-06-07 06:51:28 | 2019-06-07 07:17:47 | P  |
|   6 | 2019-06-07 06:29:39 | 2019-06-07 07:35:32 | P  |
|   6 | 2019-06-07 06:22:27 | 2019-06-07 07:45:33 | P  |
|   6 | 2019-06-07 06:09:07 | 2019-06-07 07:38:51 | P  |
|   6 | 2019-06-07 07:05:31 | 2019-06-07 07:42:46 | P  |
|   6 | 2019-06-07 07:48:01 | 2019-06-07 07:58:08 | P  |
|   6 | 2019-06-07 07:34:26 | 2019-06-07 07:53:23 | P  |
|   6 | 2019-06-07 07:26:54 | 2019-06-07 07:58:29 | P  |
|   6 | 2019-06-07 07:10:28 | 2019-06-07 07:48:48 | P  |
+-----+---------------------+---------------------+----+

1 个答案:

答案 0 :(得分:0)

试试这个:

mysql> SELECT
    DAYOFWEEK(dStart) AS DOW,
    DATE_FORMAT(dStart,'%Y-%m-%d %h:%i:%s') as dStart,
    DATE_FORMAT(dEnd,'%Y-%m-%d %h:%i:%s') as dEnd,
    cd
FROM
    `2019tblCd`
WHERE
    (
        cd IN ('P')
        AND DAYOFWEEK(dStart) BETWEEN 2
        AND 6
        AND (
            DATE_FORMAT(time(dStart),'%H:%i:%s') >= '18:00:00'
            AND DATE_FORMAT(time(dEnd),'%H:%i:%s')  <= '20:00:00'
        )
    )
ORDER BY
    STR_TO_DATE(dStart, '%Y-%m-%d %H:%i:%s') DESC
LIMIT 10;