Mysql 返回两个日期时间表达式之间的差异
在MySQL表中,我需要检查两行之间的TIMEDIFF()是否大于三分钟的值 如果TIMEDIFF()小于I,则使用1值更新禁用的xd列 这是我的桌子Mysql 返回两个日期时间表达式之间的差异,mysql,sql-update,Mysql,Sql Update,在MySQL表中,我需要检查两行之间的TIMEDIFF()是否大于三分钟的值 如果TIMEDIFF()小于I,则使用1值更新禁用的xd列 这是我的桌子 +------+---------------------+---------+-----------+ | xID | xDate_xHourMinute | xLines | xDisabled | +------+---------------------+---------+-----------+ | 1405 | 2020-0
+------+---------------------+---------+-----------+
| xID | xDate_xHourMinute | xLines | xDisabled |
+------+---------------------+---------+-----------+
| 1405 | 2020-06-21 05:24:19 | 3018122 | |
| 1424 | 2020-06-21 05:22:53 | 3018122 | |
| 1462 | 2020-06-21 05:22:03 | 3018122 | |
| 1473 | 2020-06-21 05:18:59 | 3018122 | |
| 1481 | 2020-06-21 05:18:03 | 3018122 | |
+------+---------------------+---------+-----------+
5 rows in set
我尝试了这个sql查询,但没有成功,因为表上的所有行都已更新
UPDATE `xMinutesSet` a
JOIN `xMinutesSet` b ON a.xLines = B.xLines
AND TIMEDIFF(
a.xDate_xHourMinute,
b.xDate_xHourMinute
) < 3
SET a.xDisabled = 1;
+------+---------------------+---------+-----------+
| xID | xDate_xHourMinute | xLines | xDisabled |
+------+---------------------+---------+-----------+
| 1405 | 2020-06-21 05:24:19 | 3018122 | 1 |
| 1424 | 2020-06-21 05:22:53 | 3018122 | 1 |
| 1462 | 2020-06-21 05:22:03 | 3018122 | 1 |
| 1473 | 2020-06-21 05:18:59 | 3018122 | 1 |
| 1481 | 2020-06-21 05:18:03 | 3018122 | 1 |
+------+---------------------+---------+-----------+
5 rows in set
如何解决这个问题?
有什么建议吗
我的桌子在下面
DROP TABLE IF EXISTS `xminutesset`;
CREATE TABLE `xminutesset` (
`xID` int(11) NOT NULL AUTO_INCREMENT,
`xDate_xHourMinute` datetime DEFAULT NULL,
`xLines` varchar(255) DEFAULT NULL,
`xDisabled` int(1) DEFAULT NULL,
PRIMARY KEY (`xID`)
) ENGINE=InnoDB;
-- ----------------------------
-- Records of xminutesset
-- ----------------------------
INSERT INTO `xminutesset` VALUES ('1405', '2020-06-21 05:24:19', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1424', '2020-06-21 05:22:53', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1462', '2020-06-21 05:22:03', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1473', '2020-06-21 05:18:59', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1481', '2020-06-21 05:18:03', '3018122', null);
您可以使用MySql 8.0中的LAG()
窗口函数和TIMESTAMPDIFF()
而不是TIMEDIFF()
:
您的Mysql版本是什么?请注意,
TIMEDIFF()
的输出可以是负数,在本地检查TIMEDIFF()@forpas Mysql的版本是8.0.17,在主机中是5.5.62。我需要两个版本的解决方案谢谢,但不使用MySQL 5.5.62版本请参阅我的第二个查询。
DROP TABLE IF EXISTS `xminutesset`;
CREATE TABLE `xminutesset` (
`xID` int(11) NOT NULL AUTO_INCREMENT,
`xDate_xHourMinute` datetime DEFAULT NULL,
`xLines` varchar(255) DEFAULT NULL,
`xDisabled` int(1) DEFAULT NULL,
PRIMARY KEY (`xID`)
) ENGINE=InnoDB;
-- ----------------------------
-- Records of xminutesset
-- ----------------------------
INSERT INTO `xminutesset` VALUES ('1405', '2020-06-21 05:24:19', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1424', '2020-06-21 05:22:53', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1462', '2020-06-21 05:22:03', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1473', '2020-06-21 05:18:59', '3018122', null);
INSERT INTO `xminutesset` VALUES ('1481', '2020-06-21 05:18:03', '3018122', null);
UPDATE `xminutesset` a
INNER JOIN (
SELECT *,
LAG(xDate_xHourMinute) OVER (PARTITION BY xLines ORDER BY xDate_xHourMinute) prev
FROM `xminutesset`
) b
ON a.xID = b.xID
AND TIMESTAMPDIFF(MINUTE, b.prev, b.xDate_xHourMinute) < 3
SET a.xDisabled = 1;
UPDATE `xminutesset` a
INNER JOIN (
SELECT x.*,
(
SELECT MAX(xDate_xHourMinute)
FROM `xminutesset`
WHERE `xLines` = x.`xLines` AND xDate_xHourMinute < x.xDate_xHourMinute
) prev
FROM `xminutesset` x
) b
ON a.xID = b.xID
AND TIMESTAMPDIFF(MINUTE, b.prev, b.xDate_xHourMinute) < 3
SET a.xDisabled = 1;
| xID | xDate_xHourMinute | xLines | xDisabled |
| ---- | ------------------- | ------- | --------- |
| 1405 | 2020-06-21 05:24:19 | 3018122 | 1 |
| 1424 | 2020-06-21 05:22:53 | 3018122 | 1 |
| 1462 | 2020-06-21 05:22:03 | 3018122 | |
| 1473 | 2020-06-21 05:18:59 | 3018122 | 1 |
| 1481 | 2020-06-21 05:18:03 | 3018122 | |