簡體   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