在 FROM 子句 MySql 8 中指定更新目标 table

Specify target table for update in FROM clause MySql 8

在 MySQL 数据库上我有两个表

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

当我在 tmp2 上获得相同 sNamesDate_sHour 高于 tmp1sDate_sHour 我需要更新列 sValueN

return 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 

因为 tmp2 的日期 2019-05-08 09:36:14 高于 tmp12019-04-27 14:00:52 tmp2 的日期 2019-05-08 09:36:14 小于 tmp1

2019-05-08 09:39:19

我试过使用 MySQL 8.0.17

尝试运行以下MYSQL更新语句

时出现编译错误
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> 

我不知道问题出在哪里。

拜托,你能帮帮我吗?

下面的结构表 tmp1tmp2

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
;