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