使用 updateAll() 使用相同 table 的另一列值更新 table 列
Update table column with another column values of the same table using updateAll()
我有一个 table 包含这两个真实字段 current
和 origin
.
current
值定期更新。我想写一个重置脚本:对于每一行,我想把 origin
值放在 current
值中。
在 MySQL 中可以使用此查询:
update MyTable set current = origin
我试着用查询构建器在 Yii2 中写这个:
return $this->updateAll(['current' => 'origin']);
但这不起作用,因为 origin
被解释为字符串并且所有行都更新为值 0
。
那么我如何使用 updateAll()
通过另一个字段的值更新字段值?
像这样将 origin
包裹在 yii\db\Expression 中:
use yii\db\Expression;
...
return $this->updateAll(['current' => new Expression('origin')]);
结果如预期。
我有一个 table 包含这两个真实字段 current
和 origin
.
current
值定期更新。我想写一个重置脚本:对于每一行,我想把 origin
值放在 current
值中。
在 MySQL 中可以使用此查询:
update MyTable set current = origin
我试着用查询构建器在 Yii2 中写这个:
return $this->updateAll(['current' => 'origin']);
但这不起作用,因为 origin
被解释为字符串并且所有行都更新为值 0
。
那么我如何使用 updateAll()
通过另一个字段的值更新字段值?
像这样将 origin
包裹在 yii\db\Expression 中:
use yii\db\Expression;
...
return $this->updateAll(['current' => new Expression('origin')]);
结果如预期。