ERROR 1093(HY000):您不能在FROM子句中为更新指定目标表'a'

时间:2012-11-02 01:26:09

标签: mysql sql hql

我有这个查询

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-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)