Jump to content

Recommended Posts

Hey,

 

I just got a mysql query error but no idea what it means ... my query is:

 

mysql_query("UPDATE user_data SET Proposal = 0 WHERE Proposal NOT IN (SELECT UserID FROM user_data)") or die(mysql_error());

 

Yet its not happy with it as i get this error:

You can't specify target table 'user_data' for update in FROM clause

 

The idea is if your proposal has the id of a user that is no longer in the database then set proposal to 0.

 

=/

Link to comment
https://forums.phpfreaks.com/topic/214574-what-does-this-error-mean/
Share on other sites

Hi

 

MySQL doesn't support subqueries in updates where the subquery is selecting from the same table as the update is updating.

 

From the Mysql site (at the bottom):-

 

http://dev.mysql.com/tech-resources/articles/4.1/subqueries.html

 

All the best

 

Keith

Hi

 

You can you a JOIN in an UPDATE and this might solve the issue you are having.

 

Something like this

 

UPDATE user_data a LEFT OUTER JOIN user_data b ON a.Proposal = b.UserID SET a.Proposal = 0 WHERE b.UserID IS NULL

 

All the best

 

Keith

Yes, although if kickstart's method doesn't work, you can probably work around the issue using a temp table.  The best way is to use engine=memory. 

 

create table t_orphans engine=memory as SELECT Proposal FROM user_data WHERE Proposal NOT IN (Select distinct UserID FROM user_data);
update user_data u, t_orphans t set Proposal = 0 WHERE u.Proposal = t.Proposal;
drop table t_orphans;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.