当前位置:网站首页>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 .
边栏推荐
- Startup process of uboot:
- The ninth Operation Committee meeting of dragon lizard community was successfully held
- NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
- COMSOL -- establishment of 3D graphics
- shell脚本文件遍历 str转数组 字符串拼接
- 跨境电商是啥意思?主要是做什么的?业务模式有哪些?
- 不要再说微服务可以解决一切问题了!
- 力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
- OneForAll安装使用
- Msfconsole command encyclopedia and instructions
猜你喜欢
随机推荐
go语言学习笔记-分析第一个程序
Modulenotfounderror: no module named 'scratch' ultimate solution
Bracket matching problem (STL)
[JS learning notes 54] BFC mode
如何通俗理解超级浏览器?可以用于哪些场景?有哪些品牌?
数据库三大范式
How did the situation that NFT trading market mainly uses eth standard for trading come into being?
How does redis implement multiple zones?
COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
基于OpenHarmony的智能金属探测器
Stop saying that microservices can solve all problems!
Detailed explanation of MATLAB cov function
idea设置打开文件窗口个数
Characteristics and electrical parameters of DDR4
百问百答第45期:应用性能探针监测原理-node JS 探针
MySQL 巨坑:update 更新慎用影响行数做判断!!!
In the last process before the use of the risk control model, 80% of children's shoes are trampled here
871. Minimum Number of Refueling Stops
![[crawler] Charles unknown error](/img/82/c36b225d0502f67cd04225f39de145.png)



![[office] eight usages of if function in Excel](/img/ce/ea481ab947b25937a28ab5540ce323.png)




