[英]Update table column with another column values of the same table using updateAll()
我有一个表包含这两个真实的current
和origin
字段。
current
值定期更新。 我想编写一个重置脚本:对于每一行,我想将origin
值放在current
值中。
在MySQL中 ,可以使用此查询:
update MyTable set current = origin
我尝试使用查询生成器在Yii2中编写此代码:
return $this->updateAll(['current' => 'origin']);
但这不起作用,因为origin
被解释为字符串,所有行都使用值0
更新。
那么我如何使用updateAll()
通过另一个字段的值更新字段值?
在yii \\ db \\ Expression中包装origin
如下:
use yii\db\Expression;
...
return $this->updateAll(['current' => new Expression('origin')]);
结果将如预期的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.