溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql實現(xiàn)upsert

發(fā)布時間:2020-07-12 03:45:15 來源:網(wǎng)絡(luò) 閱讀:5606 作者:hgditren 欄目:軟件技術(shù)
  • upsert(update or insert), 即更新或?qū)懭搿?/li>
  • MySQL中實現(xiàn)upsert操作方式:
    思路:通過判斷插入的記錄里是否存在主鍵索引或唯一索引沖突,來決定是插入還是更新。當(dāng)出現(xiàn)主鍵索引或唯一索引沖突時則進(jìn)行update操作,否則進(jìn)行insert操作。
    實現(xiàn):使用 ON DUPLICATE KEY UPDATE

來看看下面具體實現(xiàn)過程。

一、準(zhǔn)備數(shù)據(jù)表
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為聯(lián)合唯一索引。

二、寫入初始數(shù)據(jù)
insert into test.demo(a,b,c,d,e,f) values(1,1,1,1,1,1);

此時存在由abc散列組成唯一索引數(shù)據(jù):1,1,1。

三、進(jìn)一步實現(xiàn)
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;
因為已經(jīng)存在由abc三列組成唯一索引數(shù)據(jù):1,1,1,本次又寫入demo(a,b,c,d,e,f) values(1,1,1,2,2,2),會造成唯一索引沖突。
因此,會觸發(fā)ON DUPLICATE KEY 后面的 UPDATE a=2,b=2,c=3,d=4,e=5,f=6操作。

至此,已經(jīng)實現(xiàn)upsert功能。請記住 ON DUPLICATE KEY UPDATE的用法。

實現(xiàn)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;
    }
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI