gpt4 book ai didi

mysql - MySQL 数据库的间隔时间

转载 作者:行者123 更新时间:2023-11-29 15:54:04 25 4
gpt4 key购买 nike

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

我启动了这个 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 |
+-----+---------------------+---------------------+----+

最佳答案

试试这个:

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;

关于mysql - MySQL 数据库的间隔时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com