在 Yii 2.0 中,迁移 MySQL 数据库中的 A 表中的一列数据至 B 表中的一列数据,基于一条 SQL 的实现
1、参考网址:http://www.shuijingwanwq.com/2019/08/15/3427/
UPDATE `cpa_channel_app_source`, `cpa_weibo_weibo_connect_web_app_user` SET `cpa_channel_app_source`.`permission` = `cpa_weibo_weibo_connect_web_app_user`.`permission` WHERE `cpa_channel_app_source`.`id` = `cpa_weibo_weibo_connect_web_app_user`.`channel_app_source_id`;
2、更新表:channel_app_source 中的字段:permission 的值为:3,如图1
3、基于数据库迁移命令,生成如上所示的一条 SQL 的实现,编辑 \console\migrations\m190815_020034_permission.php
<?php use yii\db\Expression; use yii\db\Migration; /** * Class m190815_020034_permission */ class m190815_020034_permission extends Migration { /** * {@inheritdoc} */ public function safeUp() { $this->update('{{%channel_app_source}}, {{%weibo_weibo_connect_web_app_user}}', ['{{%channel_app_source}}.permission' => new Expression('{{%weibo_weibo_connect_web_app_user}}.permission')], ['{{%channel_app_source}}.id' => new Expression('{{%weibo_weibo_connect_web_app_user}}.channel_app_source_id')]); } /** * {@inheritdoc} */ public function safeDown() { echo "m190815_020034_permission cannot be reverted.\n"; return false; } /* // Use up()/down() to run migration code without a transaction. public function up() { } public function down() { echo "m190815_020034_permission cannot be reverted.\n"; return false; } */ }
4、执行数据库迁移命令,如图2
PS E:\wwwroot\channel-pub-api> ./yii migrate Yii Migration Tool (based on Yii v2.0.15.1) Total 1 new migration to be applied: m190815_020034_permission Apply the above migration? (yes|no) [no]:yes *** applying m190815_020034_permission > update {{%channel_app_source}}, {{%weibo_weibo_connect_web_app_user}} ... done (time: 0.092s) *** applied m190815_020034_permission (time: 0.190s) 1 migration was applied. Migrated up successfully.
5、查看表:channel_app_source 中的字段:permission 的值为:2,符合预期,如图3
近期评论