我试图运行如下的CASE语句:
UPDATE table SET
COL1 = CASE WHEN COL1 = '0' then 'something' else COL1 end,
COL2 = CASE WHEN COL2 = '0' then 'something' else COL2 end,
COL3 = CASE WHEN COL3 = '0' then 'something' else COL3 end
WHERE ID = '$id_example'
但我只想要第一个CASE" TRUE"执行。
编辑:
UPDATE utskick SET
DELIMITER //
CREATE FUNCTION f()
BEGIN
CASE
WHEN datum_1 = '0000-00-00' THEN datum_1 = '2020-01-01' ELSE datum_1//
WHEN datum_2 = '0000-00-00' THEN datum_2 = '2020-01-01' ELSE datum_2//
WHEN datum_3 = '0000-00-00' THEN datum_3 = '2020-01-01' ELSE datum_3//
END CASE //
END //
DELIMITER;
CALL f()
WHERE kund_id = '14'
编辑2:
UPDATE table SET
CASE
date_1 WHEN date_1 = '0000-00-00' THEN '2020-01-01'
ELSE
CASE date_2 WHEN date_2 = '0000-00-00' THEN '2020-01-01'
ELSE
CASE date_3 WHEN date_3 = '0000-00-00' THEN '2020-01-01'
END
END
END
WHERE id = 23
我收到此错误:
答案 0 :(得分:0)
如果要使用CASE语句,则应将其放在函数中。类似的东西:
DELIMITER //
CREATE FUNCTION doSomething(col varchar(3))
BEGIN
CASE
WHEN col = 'col1' THEN dothis //
WHEN col = 'col2' THEN dothat //
WHEN col = 'col3' THEN someotherthing //
END CASE //
END //
DELIMITER ;
调用函数如:
CALL doSomething(@parameter);
还有更多信息信息here。
答案 1 :(得分:0)
使用嵌套的CASE语句,如下所示:
CASE COL1 WHEN '0' THEN 'something1'
ELSE
CASE COL2 WHEN '0' THEN 'something2'
ELSE
CASE WHEN COL3 = '0' THEN 'something3'
END
END
END
答案 2 :(得分:0)
尝试
WHEN (COL3 = '0' AND NOT (COL1 = '0' OR COL2 = '0')) ...
WHEN (COL2 = '0' AND NOT (COL1 = '0')) ...
WHEN COL1 = '0' ...
所以只有想要的时候才是真的