当前位置:网站首页>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 .
边栏推荐
- 7.2 daily study 4
- 【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
- 【Office】Excel中IF函数的8种用法
- 基于OpenHarmony的智能金属探测器
- COMSOL--建立几何模型---二维图形的建立
- 管理多个Instagram帐户防关联小技巧大分享
- 龙蜥社区第九次运营委员会会议顺利召开
- AutoCAD -- mask command, how to use CAD to locally enlarge drawings
- Stop saying that microservices can solve all problems!
- 解决grpc连接问题Dial成功状态为TransientFailure
猜你喜欢
【Office】Excel中IF函数的8种用法
【爬虫】wasm遇到的bug
DDRx寻址原理
7.2每日学习4
OneForAll安装使用
Ddrx addressing principle
Huawei equipment configures channel switching services without interruption
Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report
无密码身份验证如何保障用户隐私安全?
【广告系统】增量训练 & 特征准入/特征淘汰
随机推荐
Modulenotfounderror: no module named 'scratch' ultimate solution
Cdga | six principles that data governance has to adhere to
7.2 daily study 4
解决访问国外公共静态资源速度慢的问题
sklearn模型整理
Cron表达式(七子表达式)
Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
[LeetCode] Wildcard Matching 外卡匹配
基础篇——REST风格开发
Startup process of uboot:
项目总结笔记系列 wsTax KT Session2 代码分析
pytorch训练进程被中断了
The ninth Operation Committee meeting of dragon lizard community was successfully held
Home office things community essay
【爬虫】charles unknown错误
idea设置打开文件窗口个数
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
Summary of thread and thread synchronization under window
deepfake教程
Beego cross domain problem solution - successful trial