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

会有一股神秘感。 提交于 2019-12-01 08:12:32

问题


I have this query

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)

and table look like this.

+----------------+--------------+------+-----+---------+----------------+
| 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    |                |
+----------------+--------------+------+-----+---------+----------------+

When I execute this query I get the below error:

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

I can create temporary table and keep the result of the sub-query and then do the UPDATE. But I don't want to do in this way. Can someone suggest me better way of doing this?


回答1:


You are using the alias "a" but you never define it.

Perhaps this would work:

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



回答2:


Or perhaps this will do the trick:

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)


来源:https://stackoverflow.com/questions/13188002/error-1093-hy000-you-cant-specify-target-table-a-for-update-in-from-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!