[英]How do I use a case and an if statement inside an update…set in mysql?
This is what I'm trying to do: 这就是我想要做的:
UPDATE
my_table
SET
column_a = (
SELECT
CASE
WHEN column_a is null THEN 20
WHEN column_a < 10 THEN column_a
WHEN column_a = 10 THEN 20
ELSE MIN((column_a, 20))
END
INTO @var_a
/* I want to do an if...else now */
IF(var_a > UNIX_TIMESTAMP(), null, var_a)
)
WHERE
column_b = 'something'
How would I do something like this? 我该怎么做? Thanks!
谢谢!
I think you can use a nested case
to do this: 我认为您可以使用嵌套的
case
来做到这一点:
UPDATE
my_table
SET
column_a = (
case
when (@var_a := (
case
when column_a is null then 20
when column_a < 10 then column_a
when column_a = 10 then 20
else least(column_a, 20)
end)) <= unix_timestamp() then @var_a
else null
end)
WHERE
column_b = 'something';
Notice that min()
is an aggregate function and you can't use it to get the minimum of the inputs: You need to use least()
. 注意,
min()
是一个聚合函数 ,您不能使用它来获取最少的输入:您需要使用minimum least()
。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.