3

我有这个查询

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。但我不想这样做。有人可以建议我这样做的更好方法吗?

4

2 回答 2

4

您正在使用别名“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
于 2012-11-02T01:58:39.970 回答
-1

或者也许这可以解决问题:

UPDATE trh_adminLoginDate a

SET    superseded = true 

WHERE EXISTS 
      (SELECT * FROM trh_adminLoginDate WHERE adminId = a.adminId AND loginDate > a.loginDate AND clientPlatform = a.clientPlatform)
于 2012-11-02T02:06:25.953 回答