小编典典

错误1093(HY000):您无法在FROM子句中指定目标表'a'用于更新

sql

我有这个查询

UPDATE trh_adminLoginDate SET superseded = true WHERE EXISTS 
 (SELECT * FROM trh_adminLoginDate AS a2 WHERE a2.adminId = a.adminId AND a2.loginDate > a.loginDate AND a2.clientPlatform = a.clientPlatform)

和桌子看起来像这样。

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| dateCreated    | datetime     | NO   |     | NULL    |                |
| version        | int(11)      | NO   |     | NULL    |                |
| dateModified   | datetime     | NO   |     | NULL    |                |
| adminId        | bigint(20)   | NO   | MUL | NULL    |                |
| clientPlatform | varchar(255) | YES  |     | NULL    |                |
| loginDate      | datetime     | YES  |     | NULL    |                |
| superseded     | tinyint(1)   | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

当我执行此查询时,出现以下错误:

ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause

我可以创建临时表并保留子查询的结果,然后执行UPDATE。但是我不想这样做。有人可以建议我这样做的更好方法吗?


阅读 103

收藏
2021-04-07

共1个答案

小编典典

您正在使用别名“ a”,但从未定义过它。

也许这会工作:

UPDATE  trh_adminLoginDate a

        JOIN trh_adminLoginDate AS a2 
        ON a2.adminId = a.adminId 
        AND a2.loginDate > a.loginDate 
        AND a2.clientPlatform = a.clientPlatform

SET a.superseded = true
2021-04-07