繁体   English   中英

使用updateAll()使用同一个表的其他列值更新表列

[英]Update table column with another column values of the same table using updateAll()

我有一个表包含这两个真实的currentorigin字段。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM