MySQL批量Insert應用ON DUPLICATE KEY UPDATE
也不清楚是設計的問題,還是實現的問題.
總之最後到我這裡,是一個很奇怪的需求.
award_credit_room這個表,存放使用者送禮的記錄
award_credit 是使用者送禮產生的積分記錄
award_credit_room的資料經過彙總之後,需要批量Insert到award_credit表,
如果沒有記錄,則Insert,存在記錄則Update
使用MySQL自定義變數,實現 insert..select...ON DUPLICATE KEY UPDATE
該功能使用如下SQL
set @a:=0;
set @b:=0;
insert into award_credit ( credits, vvid, CreditsTotal )
select @a:=sum(CreditChange) ,VVID,sum(CreditChange)
from award_credit_room r
where awardActId = 23 and Status = 2 group by VVID
ON DUPLICATE KEY UPDATE credits = credits + @a, creditsTotal = creditsTotal +@a;
在執行之前的彙總查詢結果
select @a:=sum(CreditChange) ,VVID,sum(CreditChange)
from award_credit_room r
where awardActId = 23 and Status = 2 group by VVID
在SQL執行之前的award_credit表資料
在SQL執行之後的award_credit表資料
在實驗的過程中,有一個優化的想法,既然兩個欄位都是sum聚合,能不能使用自定義變數計算一次呢,
實際上是行不通的,自定義變數的執行順序和變數的位置並不是嚴格固定的.
但是也有變通的方法
set @a:=0;
insert into award_credit ( credits, vvid, CreditsTotal )
select @a:=s,vvid,s from
(
select sum(CreditChange) s,VVID from award_credit_room r where awardActId = 23 and Status = 2 group by VVID
) t1
ON DUPLICATE KEY UPDATE credits = credits + @a, creditsTotal = creditsTotal +@a;
總之最後到我這裡,是一個很奇怪的需求.
award_credit_room這個表,存放使用者送禮的記錄
award_credit 是使用者送禮產生的積分記錄
award_credit_room的資料經過彙總之後,需要批量Insert到award_credit表,
如果沒有記錄,則Insert,存在記錄則Update
使用MySQL自定義變數,實現 insert..select...ON DUPLICATE KEY UPDATE
該功能使用如下SQL
set @a:=0;
set @b:=0;
insert into award_credit ( credits, vvid, CreditsTotal )
select @a:=sum(CreditChange) ,VVID,sum(CreditChange)
from award_credit_room r
where awardActId = 23 and Status = 2 group by VVID
ON DUPLICATE KEY UPDATE credits = credits + @a, creditsTotal = creditsTotal +@a;
select @a:=sum(CreditChange) ,VVID,sum(CreditChange)
from award_credit_room r
where awardActId = 23 and Status = 2 group by VVID
在SQL執行之前的award_credit表資料
在SQL執行之後的award_credit表資料
在實驗的過程中,有一個優化的想法,既然兩個欄位都是sum聚合,能不能使用自定義變數計算一次呢,
實際上是行不通的,自定義變數的執行順序和變數的位置並不是嚴格固定的.
但是也有變通的方法
set @a:=0;
insert into award_credit ( credits, vvid, CreditsTotal )
select @a:=s,vvid,s from
(
select sum(CreditChange) s,VVID from award_credit_room r where awardActId = 23 and Status = 2 group by VVID
) t1
ON DUPLICATE KEY UPDATE credits = credits + @a, creditsTotal = creditsTotal +@a;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1610013/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql INSERT ... ON DUPLICATE KEY UPDATEMySql
- MySQL insert on duplicate key update 死鎖MySql
- MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE語句說明MySql
- MySQL 關於 INSERT INTO...ON DUPLICATE KEY UPDATE 的使用MySql
- MySQL_插入更新 ON DUPLICATE KEY UPDATEMySql
- BUG: pymysql executemany不支援insert on duplicate key updateMySql
- 翻譯:insert on duplicate key update(已提交到MariaDB官方手冊)
- mysql實現merge功能之DUPLICATE key UPDATE語法MySql
- REPLACE與INSERT INTO ... ON DUPLICATE KEY總結
- mybatis 批量新增insert、更新update詳解MyBatis
- Q:[Vue warn]: Duplicate keys detected: ‘PAYACT‘. This may cause an update error.VueError
- mysql update join,insert select 語法MySql
- mysql操作命令梳理(2)-alter(update、insert)MySql
- mysql innodb新建索引堵塞update ,insert,deleteMySql索引delete
- vue報錯之Duplicate keys detected: '0'. This may cause an update error.VueError
- MySQL pt-duplicate-key-checker工具使用初探MySql
- mysql 在delete、insert、update 時,page的變化MySqldelete
- MySQL之資料的insert-delete-update操作MySqldelete
- MYSQL RC模式insert update 可能死鎖的情況MySql模式
- MySQL 拼接Insert批量同步異構表資料MySql
- MySQL使用pt-duplicate-key-checker找出冗餘、重複索引MySql索引
- MySQL 建立外來鍵報錯Can't write; duplicate key in tableMySql
- Default Locking for INSERT, UPDATE, DELETE, and SELECT ... FOR UPDATE (351)delete
- INSERT高階應用
- insert:key too large to index…Index
- MYSQL報1022錯誤:Can't write;duplicate key in table '.....'MySql
- 34、VIEW可以insert,delete,update.Viewdelete
- insert批量插入優化方案優化
- 使用JDBC時,加速批量insertJDBC
- MySQL批量insert效率對比SQL
- JDBC批量Insert深度優化JDBC優化
- MyBatis Batch Update Exception使用foreach批量update出錯MyBatisException
- sql server merge 做insert和updateSQLServer
- Window Application has "update" key wordsAPP
- 批量刪除 redis keysRedis
- Oracle中 Update和insert結合語法Oracle
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- sql server 帶有OUTPUT的INSERT,DELETE,UPDATESQLServerdelete