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

Search and Replace a JSON Field Value in MySQL 5.6 With a Single SQL Query in Yii 2.0

In the table pub_log, the data structure of the field data is json. The formatted data is as follows

1. In the table pub_log, the type of the field data is text, as shown in Figure 1.

In the table pub_log, the type of the field data is text
Figure 1

2. In the table pub_log, the data structure of the field data is json. The formatted data is as follows, as shown in Figure 2.

In the table pub_log, the data structure of the field data is json. The formatted data is as follows
Figure 2
Plaintext


{"channel_app_source_uuid":"3efd0cbabf2a11e98f9d54ee75d2ebc1","channel_app_task_uuid":"569d5ddebf2a11e993cb54ee75d2ebc1","channel_code":"weibo","channel_type_code":"weibo_weibo_connect_web","channel_app_task_status":6}


{
  "channel_app_source_uuid": "3efd0cbabf2a11e98f9d54ee75d2ebc1",
  "channel_app_task_uuid": "569d5ddebf2a11e993cb54ee75d2ebc1",
  "channel_code": "weibo",
  "channel_type_code": "weibo_weibo_connect_web",
  "channel_app_task_status": 6
}

3. The current requirement is that if “channel_app_task_status”: 6, it needs to be changed to “channel_app_task_status”: 7. That is, search for (“channel_app_task_status”: 6) and replace it with (“channel_app_task_status”: 7). Therefore, the final execution result should be as follows:

Plaintext


{"channel_app_source_uuid":"3efd0cbabf2a11e98f9d54ee75d2ebc1","channel_app_task_uuid":"569d5ddebf2a11e993cb54ee75d2ebc1","channel_code":"weibo","channel_type_code":"weibo_weibo_connect_web","channel_app_task_status":7}


{
  "channel_app_source_uuid": "3efd0cbabf2a11e98f9d54ee75d2ebc1",
  "channel_app_task_uuid": "569d5ddebf2a11e993cb54ee75d2ebc1",
  "channel_code": "weibo",
  "channel_type_code": "weibo_weibo_connect_web",
  "channel_app_task_status": 7
}

4. Execute the SQL, which affects 1 row, as shown in Figure 3. The execution result shows that the data in the field data of the table pub_log has been modified to (“channel_app_task_status”: 7), which meets expectations, as shown in Figure 4.

Execute the SQL, which affects 1 row
Figure 3

 

The execution result shows that the data in the field data of the table pub_log has been modified to ("channel_app_task_status": 7), which meets expectations
Figure 4
SQL

UPDATE `cpa_pub_log` SET `cpa_pub_log`.`data` = REPLACE(`cpa_pub_log`.`data`, '"channel_app_task_status":6', '"channel_app_task_status":7');

5. Based on the database migration command, generate the implementation of the SQL shown above. Edit \console\migrations\m190814_093631_have_pub_number_and_task_status.php

PHP
<?php
 
use yii\db\Expression;
use yii\db\Migration;
 
/**
 * Class m190814_093631_have_pub_number_and_task_status
 */
class m190814_093631_have_pub_number_and_task_status extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->update('{{%pub_log}}', ['data' => new Expression('REPLACE([[data]], \'"channel_app_task_status":6\', \'"channel_app_task_status":7\')')]);
    }
 
    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m190814_093631_have_pub_number_and_task_status cannot be reverted.\n";
 
        return false;
    }
 
    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {
 
    }
 
    public function down()
    {
        echo "m190814_093631_have_pub_number_and_task_status cannot be reverted.\n";
 
        return false;
    }
    */
}
 

6. After resetting it to 6, execute the database migration command, as shown in Figure 5.

After resetting it to 6, execute the database migration command
Figure 5
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:
        m190814_093631_have_pub_number_and_task_status

Apply the above migration? (yes|no) [no]:yes
*** applying m190814_093631_have_pub_number_and_task_status
    > update {{%pub_log}} ... done (time: 0.010s)
*** applied m190814_093631_have_pub_number_and_task_status (time: 0.022s)


1 migration was applied.

Migrated up successfully.

7. Check that the data in the field data of the table pub_log has been modified to (“channel_app_task_status”: 7), which meets expectations.

 

Plaintext

{"channel_app_source_uuid":"3efd0cbabf2a11e98f9d54ee75d2ebc1","channel_app_task_uuid":"569d5ddebf2a11e993cb54ee75d2ebc1","channel_code":"weibo","channel_type_code":"weibo_weibo_connect_web","channel_app_task_status":7}

{
  "channel_app_source_uuid": "3efd0cbabf2a11e98f9d54ee75d2ebc1",
  "channel_app_task_uuid": "569d5ddebf2a11e993cb54ee75d2ebc1",
  "channel_code": "weibo",
  "channel_type_code": "weibo_weibo_connect_web",
  "channel_app_task_status": 7
}

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

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

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

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

关于我  ·  GitHub  ·  邮件联系