2

I am trying to update the user_status filed with condition:

update pm_users 
    set user_status = if ( 
      (select u.user_status from pm_users u where u.user_id = 3
      ) = '1', '0', '1' )
    where user_id = 3

means if user_status = 1 then update the status with 0 and if user status is 0 then with 1.

I am getting the error: You can't specify target table 'pm_users' for update in FROM clause

I think it means i can't use this query like above for the same table? I am not sure.
Please help me to move on the right way, and make me correct.

3
  • 1
    Are you meant to update the status of user_id = 3 based on the status of user_id = 2? Commented Aug 12, 2013 at 5:26
  • @astander sorry for the little typo mistake, both the user id is same, i have updated my question. Commented Aug 12, 2013 at 5:30
  • As far as the user_id condition is same you can try Case When user_status = 0 Then 1 else 0 end Commented Aug 12, 2013 at 5:33

2 Answers 2

11

try the following

update pm_users 
    set user_status = case when user_status = 0 then 1 else 0 end
    where user_id = 3
Sign up to request clarification or add additional context in comments.

Comments

0

Rather then maybe try CASE

update pm_users 
    set user_status = CASE WHEN user_status = '1' THEN '0' ELSE '1' END
WHERE user_id = 3

SQL Fiddle DEMO

1 Comment

OHH, you mean i don't have to user select query for the same table

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.