class LegacyHashServiceProvider extends ServiceProvider
{
protected $defer = true;
public function register()
{
$this->app->singleton('hash', \App\Classes\LegacyHash::class);
}
public function provides()
{
return ['hash'];
}
}
我想用条件更新列。
在第一行,
Name id Col1 Col2 Col3
Row1 1 6 1 A
Row2 2 2 3 B
Row3 3 9 5 B
Row4 4 16 8 C
在第二行,
update col2 = Col1+0 if Col3 = A
OR
col2 = Col1-0 if Col3 = B
OR
col2 = Col1*0 if Col3 = C
同样适用于第三行
答案 0 :(得分:1)
我认为你可以用case语句和变量做你想做的事情:
set @prev_col2 = 0;
update t
set col2 = (case when (@col2 := @prev_col2) = NULL then -1 -- never happens
when (@prev_col2 := col2) = NULL then -1 -- never happens
when col3 = 'A' then Col1 + @col2
when col3 = 'B' then Col1 - @col2
when Col3 = 'C' then col1 * @col2
end)
order by id;