没有不值得去解决的问题,也没有不值得去学习的技术!

Migrate a Column From Table A to Table B in MySQL Using a Single SQL Query in Yii 2.0

Execute the database migration command

1. Reference URL: https://www.shuijingwanwq.com/2019/08/15/3427/

SQL

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. Update the value of the field: permission in the table: channel_app_source to: 3, as shown in Figure 1

Update the value of the field: permission in the table: channel_app_source to: 3
Figure 1

3. Based on the database migration command, generate the implementation of a SQL statement as shown above, and edit \console\migrations\m190815_020034_permission.php

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. Execute the database migration command, as shown in Figure 2

Execute the database migration command
Figure 2
Plaintext

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. Check that the value of the field: permission in the table: channel_app_source is: 2, which meets expectations, as shown in Figure 3

Check that the value of the field: permission in the table: channel_app_source is: 2, which meets expectations
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系