在FROM子句MySql 8中指定更新的目标表
在MySQL数据库上,我有两个表在FROM子句MySql 8中指定更新的目标表,mysql,nested-query,Mysql,Nested Query,在MySQL数据库上,我有两个表 tmp1 mysql> SELECT * FROM `tmp1`; +-----+---------------------+-----------+----------------+--------+ | sID | sDate_sHour | sName | sStatus | sValue | +-----+---------------------+-----------+----------------+--
tmp1
mysql> SELECT * FROM `tmp1`;
+-----+---------------------+-----------+----------------+--------+
| sID | sDate_sHour | sName | sStatus | sValue |
+-----+---------------------+-----------+----------------+--------+
| 1 | 2019-04-27 14:00:52 | user76681 | not registered | NULL |
| 2 | 2019-05-08 09:39:19 | user76681 | not registered | NULL |
+-----+---------------------+-----------+----------------+--------+
2 rows in set
和tmp2
mysql> SELECT * FROM `tmp2`;
+-----+---------------------+-----------+------------+
| sID | sDate_sHour | sName | sStatus |
+-----+---------------------+-----------+------------+
| 1 | 2019-05-08 09:36:14 | user76681 | registered |
+-----+---------------------+-----------+------------+
1 row in set
当我为相同的sName
打开tmp2
时,sDate\u sHour
高于tmp1的sDate\u sHour
时,我需要用N
值更新列sValue
更新后返回tmp1
mysql> SELECT * FROM `tmp1`;
+-----+---------------------+-----------+----------------+--------+
| sID | sDate_sHour | sName | sStatus | sValue |
+-----+---------------------+-----------+----------------+--------+
| 1 | 2019-04-27 14:00:52 | user76681 | not registered | N |
| 2 | 2019-05-08 09:39:19 | user76681 | not registered | NULL |
+-----+---------------------+-----------+----------------+--------+
2 rows in set
因为2019-05-08 09:36:14
fromtmp2
的日期高于2019-04-27 14:00:52
fromtmp1
来自tmp2
的日期2019-05-08 09:36:14
小于2019-05-08 09:39:19
fromtmp1
mysql> SELECT * FROM `tmp1`;
+-----+---------------------+-----------+----------------+--------+
| sID | sDate_sHour | sName | sStatus | sValue |
+-----+---------------------+-----------+----------------+--------+
| 1 | 2019-04-27 14:00:52 | user76681 | not registered | NULL |
| 2 | 2019-05-08 09:39:19 | user76681 | not registered | NULL |
+-----+---------------------+-----------+----------------+--------+
2 rows in set
我尝试过使用MySQL 8.0.17
尝试运行以下MYSQL update语句时出现编译错误
mysql> UPDATE tmp1 AS b
INNER JOIN tmp2 g ON b.sName = g.sName
SET b.sValue = 'N'
WHERE
g.sDate_sHour > (
SELECT
MAX(b.sDate_sHour)
FROM
`tmp1` t2
WHERE
t2.sName = b.sName
ORDER BY
t2.sID DESC
)
AND b.sStatus IN ('not registered')
AND b.sName = g.sName
ORDER BY
g.sDate_sHour DESC;
1093 - You can't specify target table 'b' for update in FROM clause
mysql>
我不知道是什么问题
拜托,你能帮我吗
下面的结构表tmp1
和tmp2
DROP TABLE IF EXISTS `tmp1`;
CREATE TABLE `tmp1` (
`sID` int(11) NOT NULL AUTO_INCREMENT,
`sDate_sHour` datetime DEFAULT NULL,
`sName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sStatus` varchar(255) DEFAULT NULL,
`sValue` char(1) DEFAULT NULL,
PRIMARY KEY (`sID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tmp1
-- ----------------------------
INSERT INTO `tmp1` VALUES ('1', '2019-04-27 14:00:52', 'user76681', 'not registered', null);
INSERT INTO `tmp1` VALUES ('2', '2019-05-08 09:39:19', 'user76681', 'not registered', null);
DROP TABLE IF EXISTS `tmp2`;
CREATE TABLE `tmp2` (
`sID` int(11) NOT NULL AUTO_INCREMENT,
`sDate_sHour` datetime DEFAULT NULL,
`sName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`sStatus` varchar(255) DEFAULT NULL,
PRIMARY KEY (`sID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tmp2
-- ----------------------------
INSERT INTO `tmp2` VALUES ('1', '2019-05-08 09:36:14', 'user76681', 'registered');
您也不能在更新查询中使用ORDER BY
对于错误代码,只需使用子查询
UPDATE tmp1 AS b
INNER JOIN tmp2 g ON b.sName = g.sName
SET b.sValue = 'N'
WHERE
g.sDate_sHour > (
SELECT
MAX(b.sDate_sHour)
FROM
(SELECT * FROM `tmp1`) t2
WHERE
t2.sName = b.sName
ORDER BY
t2.sID DESC
)
AND b.sStatus IN ('not registered')
AND b.sName = g.sName
;