gpt4 book ai didi

php - 使用 updateAll() 使用同一个表的另一个列值更新表列

转载 作者:可可西里 更新时间:2023-11-01 07:01:20 27 4
gpt4 key购买 nike

我有一个表,其中包含这两个真实字段 currentorigin

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')]);

结果将如预期。

关于php - 使用 updateAll() 使用同一个表的另一个列值更新表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28328897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com