从多行增加相同的列值
I'm using the blow query to update a column in multiple rows:
UPDATE table SET col =
(case
when id = 1 then 10
when id = 2 then 20
when id = 3 then 30
end)
And I know if I want to increase the col
value I should do this:
UPDATE table SET col = col+10
But it doesn't work for updating multiple rows.
I also tried:
when id = 1 then (@col := @col + 10)
But this doesn't work too.
Anyone knows how can I concatenate these two with each other and increase one column's value in multiple rows in mysql ?
我正在使用点击查询来更新多行中的列: p>
我知道如果我想增加 但它不能用于更新多个 strong>行。 但这也不起作用。 UPDATE表SET col =
(case = n当id = 1然后10
当id = 2然后20
当id = 3然后30
end)
code> pre >
col code>值,我应该这样做: p>
UPDATE table SET col = col + 10
code> pre>
我也尝试过: p>
当id = 1然后(@col:= @col + 10)
code> pre>
任何人都知道如何将这两者相互连接,并且在mysql的多行中增加一列的值? strong> p>
div>
If I understood you right:
UPDATE table SET col = col +
(case
when id = 1 then 10
when id = 2 then 20
when id = 3 then 30
end)
Looking for this?
UPDATE table SET col = if(@col is null, @col := col+10, @col)
or this?
UPDATE table SET col = if(id = 1, col+10, if(id = 2, col + 20, col + 30))