Bug #6980 | Error 1093 - You can't specify target table for update in FROM | ||
---|---|---|---|
Submitted: | 3 Dec 2004 10:20 | Modified: | 6 Feb 2014 14:22 |
Reporter: | Sanvido Federico | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S2 (Serious) |
Version: | 4.1.7 | OS: | Windows (Windows 2000) |
Assigned to: | CPU Architecture: | Any |
[3 Dec 2004 10:20]
Sanvido Federico
[3 Dec 2004 12:08]
MySQL Verification Team
Hi, Thank you for the report, but currently you can't update the same table which you use in the SELECT part. This behaviour is documented at: http://dev.mysql.com/doc/mysql/en/UPDATE.html
[4 Jul 2007 10:02]
syed mohammed
Subquery returns more than 1 row
[4 Jul 2007 10:03]
syed mohammed
Subquery returns more than 1 row
[4 Jul 2007 22:59]
Jared S
Ha! beat it, this bug should be closed... UPDATE sales AS t1, sales AS t2 SET t1.idproduct = 1 WHERE t2.id > 1
[20 Aug 2009 9:00]
Victor Sergienko
Still there in 5.1. update m set flag = 1 where id in (select max(grouped.id) from a as grouped group by year(grouped.timestamp)) No end-user reason why this shouldn't work. And it's a small excuse to have it documented (for years!)
[21 May 2010 14:34]
Otávio Souza
Simple, just alias the table and it's "solved", like this INSERT INTO groupBolaoLock (`groupId`, `groupCount`) VALUES ( ( SELECT IF( MAX(groupBolaoLockB.groupId) IS NULL, 1, IF( groupBolaoLockB.groupCount = 10, MAX(groupBolaoLockB.groupId) + 1, MAX(groupBolaoLockB.groupId) ) ) FROM groupBolaoLock groupBolaoLockB ),1) ON DUPLICATE KEY UPDATE groupCount = groupCount + 1
[22 May 2010 14:36]
Victor Sergienko
Otávio, did you see that in last comment's example the table is aliased? The bug looks really fixed, but it's not about table alias anyway. I retried it on 5.1.41, and it worked fine.
[15 Jul 2010 12:18]
Richard Carnes
Yet another reason to use Postgresql instead! I have used MySQL for years and over time I have found the limitations of it. Using Postgresql this is not an issue at all! I created a table, ran an update with a select as a filter with no problems. Also if Postgresql is rebooted, empty innodb tables do not reset their auto incremented values - caused a huge issue of overwriting data months ago! The more I use the two the more I want to use just one - Postgresql! Simply referring to the documentation when other databases behave properly is hardly an excuse! In my reasoning I would think that the DBMS should run the select and then just apply the result to the update statement - that is how all subqueries work!
[19 Jan 2011 13:22]
delete me
I have a nice workaround: Instead of UPDATE `projects` SET `parent_id`=( SELECT p.`id` FROM `projects` AS p WHERE p.`identifier`='ic' ) WHERE `identifier`='p' I write this UPDATE `projects` SET `parent_id`=( SELECT p.`id` FROM (SELECT * FROM `projects`) AS p WHERE p.`identifier`='ic' ) WHERE `identifier`='p' Hopes this helps anybody.
[8 Jul 2013 10:04]
jeki mehta
this query gives the error #1093 - You can't specify target table 'user' for update in FROM clause query is : update user set iFriends = (select count(*) as tot from friends join user as u on u.iUserId = IF(friends.iSenderId = 1,friends.iReceiverId,friends.iSenderId) where (friends.iSenderId = 1 or friends.iReceiverId = 1) and friends.eStatus = 'Accept' and u.eStatus = 'Active') where user.iUserId = '1'