0

I am trying to update a specific row in a table based on another value in that table, but I can't seem to figure out how to do it:

UPDATE users AS a SET a.val = (SELECT value FROM users WHERE userid = 4) WHERE a.userID = 1

but I am getting the error

Lookup Error - MySQL Database Error: You can't specify target table 'a' for update in FROM clause

Any ideas what I am missing here?

2 Answers 2

1

Use JOIN syntax and a non-equi join

UPDATE users a JOIN users b
    ON a.userID = 1
   AND b.userid = 4
   SET a.value = b.value

Here is SQLFiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Slightly different from SQL server, which is what I'm used to.
0

use join and subquery,

update users as a
join (select value  
      from users 
      where userid=4) as sub
set a.val=sub.val
where a.userID=1;

The subquery will retrieve the value from the users table where userid=4 and then joins this subquery with the users table where userID=1 and updates the val column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.