Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MySQL中使用IF-ELSE条件更新同一个表_Mysql_Sql - Fatal编程技术网

在MySQL中使用IF-ELSE条件更新同一个表

在MySQL中使用IF-ELSE条件更新同一个表,mysql,sql,Mysql,Sql,我正在尝试更新包含以下条件的用户\u状态: 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 表示如果user\u status=1则用0更新状态,如果user status为0则用1更新状态 我收到错误:您不能在FROM子句中为更新

我正在尝试更新包含以下条件的
用户\u状态

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
表示如果
user\u status=1
则用0更新状态,如果user status为0则用1更新状态

我收到错误:
您不能在FROM子句中为更新指定目标表“pm\U用户”

我想这意味着我不能对同一个表使用上述查询?我不确定。
请帮助我沿着正确的方向前进,并纠正我的错误。

尝试以下方法

update pm_users 
    set user_status = case when user_status = 0 then 1 else 0 end
    where user_id = 3
而不是尝试


您是否打算根据用户\u id=2的状态来更新用户\u id=3的状态?@a为这个小错误感到抱歉,这两个用户id都是相同的,我已经更新了我的问题。只要用户\u id条件相同,您可以尝试
当用户\u status=0时的情况,然后1其他0结束
OHH,你的意思是我不必为同一个表使用
选择
查询
update pm_users 
    set user_status = CASE WHEN user_status = '1' THEN '0' ELSE '1' END
WHERE user_id = 3