0
UPDATE user SET 
                tw_oauth_token=(SELECT tw_oauth_token FROM user WHERE id=27),
                tw_oauth_token_secret = (SELECT tw_oauth_token_secret FROM user WHERE id=27),
                tw_user_id = (SELECT tw_user_id FROM user WHERE id=27),
                handler = (SELECT handler FROM user WHERE id=27),
                merged=1 WHERE id=26

The idea is to select data from user table where id = 27 and update the same table where id = 26.

I am having the following error:

#1093 - You can't specify target table 'user' for update in FROM clause

Any help would be appreciated. Thanks

1

2 Answers 2

1
UPDATE  user a
        CROSS JOIN user b
SET     a.tw_oauth_token = b.tw_oauth_token,
        a.tw_oauth_token_secret = b.tw_oauth_token_secret,
        a.tw_user_id = b.tw_user_id,
        a.handler = b.handler,
        a.mrged = 1
WHERE   a.ID = 26 AND
        b.ID = 27
Sign up to request clarification or add additional context in comments.

5 Comments

I don't understand why this is a CROSS JOIN rather than a NATURAL or INNER JOIN; it seems like the salient part is that it's a self-JOIN user AS a joined to user AS b --- but where the "AS" syntactic sugar is being left out. (Personally I prefer to see the "AS" keyword, especially in discussion and documentation).
@JimDennis you cannot use INNER JOIN here since you cannot determine where the record will be joined. here's what happens with the statement above: sqlfiddle.com/#!2/11f5f/2
@JimDennis the result if you use NATURAL JOIN sqlfiddle.com/#!2/beb51/2 which doens't give you correct data.
@JimDennis lastly, I'm not fun of using AS myself as it is optional.
Thanks for the sqlfiddle links. I really need to start using that.
1

Why don't you try to create a virtual table based on your table then update the user table with SELECT statements on that View. Something like this:

CREATE VIEW view_user AS
SELECT *
FROM user;

And then use the view_user in the update.

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.