当前位置:网站首页>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 .
边栏推荐
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
- 不要再说微服务可以解决一切问题了!
- R3Live系列学习(四)R2Live源码阅读(2)
- Detailed explanation of DDR4 hardware schematic design
- 中非 钻石副石怎么镶嵌,才能既安全又好看?
- PHP中Array的hash函数实现
- In the last process before the use of the risk control model, 80% of children's shoes are trampled here
- Stop saying that microservices can solve all problems!
- 修复动漫1K变8K
猜你喜欢

Summary of thread and thread synchronization under window

Go language learning notes - analyze the first program

Intelligent metal detector based on openharmony
![[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output](/img/96/b8585205b3faf503686c5bbdcecc53.png)
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output

【广告系统】增量训练 & 特征准入/特征淘汰

紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成

Stop saying that microservices can solve all problems!

Cdga | six principles that data governance has to adhere to
![[JS learning notes 54] BFC mode](/img/47/a07084ef6064589d2eeb6f091753e0.png)
[JS learning notes 54] BFC mode

基于OpenHarmony的智能金属探测器
随机推荐
Zcmu--1390: queue problem (1)
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
Pytorch training process was interrupted
紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
AutoCAD -- mask command, how to use CAD to locally enlarge drawings
Question and answer 45: application of performance probe monitoring principle node JS probe
[LeetCode] Wildcard Matching 外卡匹配
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
Golang application topic - channel
2048游戏逻辑
项目总结笔记系列 wsTax KT Session2 代码分析
[advertising system] parameter server distributed training
解决访问国外公共静态资源速度慢的问题
使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
comsol--三维图形随便画----回转
Spark Tuning (I): from HQL to code
An error is reported in the process of using gbase 8C database: 80000305, host IPS long to different cluster. How to solve it?
COMSOL--建立几何模型---二维图形的建立