phpmyadmin IF的SQL用例?[已关闭]

xam8gpfp  于 2023-02-16  发布在  PHP
关注(0)|答案(2)|浏览(148)

2小时前关门了。
Improve this question

SELECT f.user_id, f.item_id, f.status, f.shipped,
     CASE
      WHEN f.status = 0 THEN UPDATE f SET f.status = 1 WHERE f.user_id = 1 AND f.item_id = 1
      WHEN f.status = 1 THEN UPDATE f SET f.status = 0 WHERE f.user_id = 1 AND f.item_id = 1
      ELSE 'ERROR'
    END CASE
   FROM business AS f;

您好,我想根据某个项目的值(0或1)更新其状态。但仍然出现语法错误。我是否使用了正确的条件语句,还是应该使用IF()?如果是,如何在IF()中放置另一个查询?
谢谢大家!

f2uvfpb9

f2uvfpb91#

如果要更新表中的实际数据,请使用UPDATE语句,而不是SELECT语句:

UPDATE business
SET status = 
        CASE WHEN status = 1 THEN 0 
             WHEN status = 0 THEN 1
             ELSE -1 --Likely this has to be numeric since you are setting a numeric column, right? 
             END
WHERE user_id = 1 AND item_id = 1;

运行此命令将更新business表中的记录其中user_id = 1item_id=1切换status的值,除非当前状态值不是10,在这种情况下,它会将status设置为-1。请注意,字符串'error'不能用作值,除非此列是varchar或类似的字符串类型列。

ubof19bj

ubof19bj2#

这是一个非常简单的SQL查询:

UPDATE business b
SET b.status = CASE WHEN b.status = 1 THEN 0 ELSE 1 END 
WHERE b.user_id = 1 AND b.item_id = 1;

SELECT * FROM  business;

相关问题