这是我在 MySQL 数据库上的 dotable :
+--------------+------------+----------+
| sID | sDate | sHour |
+--------------+------------+----------+
| 1440190025 | 2019-05-08 | 02:46:01 |
| 1440190019 | 2019-05-07 | 16:06:01 |
| 1440190017 | 2019-05-07 | 12:59:02 |
| 1440190013 | 2019-05-07 | 10:56:14 |
+--------------+------------+----------+
我需要在此 dotable 上选择此时隙中包括的所有行:
18:00:00-00:00:00
将开始时间减少三个小时(18:00:00的 -3小时),将最后一个小时增加3小时(在00:00:00的 +3小时) ):
-3 18:00:00 >>> 15:00:00
+3 00:00:00 >>> 03:00:00
我已经尝试过此SQL查询:
mysql> SELECT
sID,
sDate,
sHour
FROM
dotable
WHERE
sHour BETWEEN ADDTIME(
SUBSTRING_INDEX('18:00:00-00:00:00', '-', 1) ,- 30000
)
AND ADDTIME(
SUBSTRING_INDEX('18:00:00-00:00:00', '-', - 1),
30000
);
+------------+------------+----------+
| sID | sDate | sHour |
+------------+------------+----------+
| 1440190019 | 2019-05-07 | 16:06:01 |
+------------+------------+----------+
1 row in set
mysql>
此输出不显示编号为 1440190025 的 sID ,尽管 sHour 的值包含在时段时间 15:00中: 00-03:00:00 。
有人知道为什么吗?
我的下面的代码。
DROP TABLE IF EXISTS `dotable`;
CREATE TABLE `dotable` (
`sID` int(10) NOT NULL AUTO_INCREMENT,
`sDate` date DEFAULT NULL,
`sHour` varchar(255) DEFAULT NULL,
PRIMARY KEY (`sID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of dotable
-- ----------------------------
INSERT INTO `dotable` VALUES ('1440190013', '2019-05-07', '10:56:14');
INSERT INTO `dotable` VALUES ('1440190017', '2019-05-07', '12:59:02');
INSERT INTO `dotable` VALUES ('1440190019', '2019-05-07', '16:06:01');
INSERT INTO `dotable` VALUES ('1440190025', '2019-05-08', '02:46:01');
答案 0 :(得分:1)
我希望我能帮上忙。
mysql> SELECT
sID,
sDate,
sHour
FROM
`dotable`
WHERE
sHour >= ADDTIME(
SUBSTRING_INDEX('18:00:00-00:00:00', '-', 1) ,- 30000
)
OR sHour <= ADDTIME(
SUBSTRING_INDEX('18:00:00-00:00:00', '-', - 1),
30000
);
+------------+------------+----------+
| sID | sDate | sHour |
+------------+------------+----------+
| 1440190019 | 2019-05-07 | 16:06:01 |
| 1440190025 | 2019-05-08 | 02:46:01 |
+------------+------------+----------+
2 rows in set