当前位置:网站首页>MySQL statistical skills: on duplicate key update usage
MySQL statistical skills: on duplicate key update usage
2022-07-05 11:27:00 【Let God love you】
ON DUPLICATE KEY UPDATE yes mysql Special grammar of , And INSERT INTO Use it together , It means that the record will be updated as soon as it exists , Otherwise add
INSERT INTO user(userid,username,age)
VALUES(1,'ssy',20) ON DUPLICATE KEY UPDATE age = age + 1;
Perform result analysis :
- Suppose the record before this statement is not executed is like this :
userid | username | age |
---|---|---|
1 | ssy | 20 |
Case one :
If userid、username、age Any of the three fields is set PRIMARY KEY perhaps UNIQUE, Then the effect of this statement is UPDATE.
hypothesis userid It's set to PRIMARY KEY, So the one above SQL amount to :
UPDATE user SET age = age + 1 WHERE userid = 1;
userid | username | age |
---|---|---|
1 | ssy | 21 |
It should be noted that : In this case, only recognition is set to PRIMARY KEY Of userid Fields and by ON DUPLICATE KEY UPDATE Of age Field , Other fields in the statement are ignored .
The second case :
If userid、username、age None of the three fields PRIMARY KEY perhaps UNIQUE, The effect of this sentence is INSERT INTO
above SQL Statement equivalent :
INSERT INTO user(userid,username,age) VALUES(1,'ssy',20);
userid | username | age |
---|---|---|
1 | ssy | 20 |
2 | ssy | 20 |
Applicable scenario : Statistics count
INSERT INTO … ON DUPLICATE KEY UPDATE … The function of is simply , Update when records exist , Otherwise insert . This is very suitable for quantity statistics .
For example, the user operation log statistics table is called count, Field is id,username,opnum,date
- id by PRIMARY KEY
- username and date Is not repeated UNIQUE Combine , That is, as long as username and date This pair of combinations cannot be repeated
CREATE TABLE `user`(
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL DEFAULT '',
`opnum` INT(11) NOT NULL DEFAULT '0',
`date` INT(11) NOT NULL DEFAULT '0',
UNIQUE KEY `unique` (`username`, `date`)
)
The original data is :
userid | username | opnum | date |
---|---|---|---|
1 | ssy | 0 | 20150424 |
If executed :
INSERT INTO count(username,opnum,date)
VALUES('ssy',5,'20150424') ON DUPLICATE KEY UPDATE opnum = opnum + 1;
The result is :
userid | username | opnum | date |
---|---|---|---|
1 | ssy | 1 | 20150424 |
INSERT INTO count(username,opnum,date) VALUES('ssy',10,'20150425')
ON DUPLICATE KEY UPDATE opnum = opnum + 1;
INSERT INTO count(username,opnum,date)
VALUES('dlm',10,'20150425') ON DUPLICATE KEY UPDATE opnum = opnum + 1;
The result is :
userid | username | opnum | date |
---|---|---|---|
1 | ssy | 1 | 20150424 |
2 | ssy | 10 | 20150424 |
3 | dlm | 10 | 20150424 |
such , If the user ssy stay 2015 year 4 month 24 If there is any operation on this day , Just append the operand , If the user has no operation today , Then add a record of today's operation . That is, it realizes the daily statistics of multiple users .
边栏推荐
- Summary of thread and thread synchronization under window
- Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
- Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
- AutoCAD -- mask command, how to use CAD to locally enlarge drawings
- How to understand super browser? What scenarios can it be used in? What brands are there?
- [JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
- 龙蜥社区第九次运营委员会会议顺利召开
- COMSOL -- three-dimensional graphics random drawing -- rotation
- 管理多个Instagram帐户防关联小技巧大分享
- R3Live系列学习(四)R2Live源码阅读(2)
猜你喜欢
Three paradigms of database
Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
技术管理进阶——什么是管理者之体力、脑力、心力
The ninth Operation Committee meeting of dragon lizard community was successfully held
Basics - rest style development
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
DDR4的特性与电气参数
如何让全彩LED显示屏更加节能环保
【Oracle】使用DataGrip连接Oracle数据库
随机推荐
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Mysql统计技巧:ON DUPLICATE KEY UPDATE用法
跨境电商是啥意思?主要是做什么的?业务模式有哪些?
[office] eight usages of if function in Excel
高校毕业求职难?“百日千万”网络招聘活动解决你的难题
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
COMSOL--三维随便画--扫掠
MFC pet store information management system
COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
不要再说微服务可以解决一切问题了!
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
【爬虫】charles unknown错误
Three suggestions for purchasing small spacing LED display
DDRx寻址原理
Go language learning notes - analyze the first program
msfconsole命令大全,以及使用说明
R3live series learning (IV) r2live source code reading (2)
以交互方式安装ESXi 6.0
POJ 3176-Cow Bowling(DP||记忆化搜索)
基础篇——REST风格开发