mysql实现upsert

发布时间:2020-07-12 03:45:15 作者:hgditren
来源:网络 阅读:5606

来看看下面具体实现过程。

一、准备数据表
CREATE TABLE `demo` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `a` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `b` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `c` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `d` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `e` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `f` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `unq_a_b_c` (`a`,`b`,`c`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

注意:表中存在两处索引,id为主键索引,a,b,c为联合唯一索引。

二、写入初始数据
insert into test.demo(a,b,c,d,e,f) values(1,1,1,1,1,1);

此时存在由abc散列组成唯一索引数据:1,1,1。

三、进一步实现
insert into demo(a,b,c,d,e,f) values(1,1,1,2,2,2) ON DUPLICATE KEY UPDATE a=2,b=2,c=3,d=4,e=5,f=6;
因为已经存在由abc三列组成唯一索引数据:1,1,1,本次又写入demo(a,b,c,d,e,f) values(1,1,1,2,2,2),会造成唯一索引冲突。
因此,会触发ON DUPLICATE KEY 后面的 UPDATE a=2,b=2,c=3,d=4,e=5,f=6操作。

至此,已经实现upsert功能。请记住 ON DUPLICATE KEY UPDATE的用法。

实现mysql的批量更新

 insert into statistic_customer(customer_id,current_period,period_number,client_upload_bill,update_time) values
 (1,201604,100,100,1540470829),
 (314,201604,100,100,1540470829),
 (315,201604,100,100,1540470829),
 (316,201611,100,100,1540470829)
 ON DUPLICATE KEY UPDATE
 customer_id=values(customer_id),
 current_period=values(current_period),
 period_number=values(period_number),
 client_upload_bill=values(client_upload_bill),
 update_time=values(update_time)
public static function test()
    {
        $updateData = [
            [
                'customer_id' => 1,
                'current_period' => 201604,
                'period_number' => 100,
                'client_upload_bill' => 100,
                'update_time' => time(),
            ],
            [
                'customer_id' => 314,
                'current_period' => 201604,
                'period_number' => 100,
                'client_upload_bill' => 100,
                'update_time' => time(),
            ],
            [
                'customer_id' => 315,
                'current_period' => 201604,
                'period_number' => 100,
                'client_upload_bill' => 100,
                'update_time' => time(),
            ],
            ['customer_id' => 316,
                'current_period' => 201611,
                'period_number' => 100,
                'client_upload_bill' => 100,
                'update_time' => time(),
            ],

        ];

        $sql = self::buildSQL('statistic_customer', $updateData);
        DB::insert($sql);
    }

    private static function buildSQL(string $tableName, array $updateData): string
    {
        $sql = "insert into {$tableName}(";
        $keys = array_keys($updateData[0]);
        $sql .= implode(',', $keys);
        $sql .= ') values';

        $valuesStr = '';
        foreach ($updateData as $value) {
            $valuesStr .= '(' . implode(',', array_values($value)) . '),';
        }
        $sql .= rtrim($valuesStr, ',');
        $sql .= ' ON DUPLICATE KEY UPDATE ';
        $updateStr = '';
        foreach ($keys as $key) {
            $updateStr .= "{$key}=values($key),";
        }
        $sql .= rtrim($updateStr, ',');
        return $sql;
    }
推荐阅读:
  1. MHA+MySQL实现mysql高可用
  2. MySQL中实现upsert操作的原理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql upsert php

上一篇:CentOS-6.8环境下phpMyAdmin安装

下一篇:HashiCorp Consul Connect 初探

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》