当前位置:网站首页>Mysql统计技巧:ON DUPLICATE KEY UPDATE用法
Mysql统计技巧:ON DUPLICATE KEY UPDATE用法
2022-07-05 11:23:00 【讓丄帝愛伱】
ON DUPLICATE KEY UPDATE是mysql的特殊语法,与INSERT INTO一起使用,意思就是记录存在就更新,否则就添加
INSERT INTO user(userid,username,age)
VALUES(1,'ssy',20) ON DUPLICATE KEY UPDATE age = age + 1;
执行结果分析:
- 假设未执行此语句前的记录是这样:
userid | username | age |
---|---|---|
1 | ssy | 20 |
第一种情况:
如果userid、username、age三个字段中的任何一个被设置了PRIMARY KEY或者UNIQUE,那么这条语句的效果是UPDATE。
假设userid被设成了PRIMARY KEY,那么上面的SQL相当于:
UPDATE user SET age = age + 1 WHERE userid = 1;
userid | username | age |
---|---|---|
1 | ssy | 21 |
需要注意的是: 这种情况下只认被设成PRIMARY KEY的userid字段和被ON DUPLICATE KEY UPDATE的age字段,语句里的其他字段都被忽略。
第二种情况:
如果userid、username、age三个字段都不是PRIMARY KEY或者UNIQUE,那这条语句的效果是INSERT INTO
上面的SQL语句相当于:
INSERT INTO user(userid,username,age) VALUES(1,'ssy',20);
userid | username | age |
---|---|---|
1 | ssy | 20 |
2 | ssy | 20 |
适用场景: 统计计数
INSERT INTO … ON DUPLICATE KEY UPDATE … 的作用简单讲就是,当记录存在时更新,否则插入。这非常适用于数量统计。
比如用户操作日志统计表叫count,字段为id,username,opnum,date
- id 为 PRIMARY KEY
- username和date为不重复的 UNIQUE组合,也就是说只要username和date这对组合不能重复
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`)
)
原数据为:
userid | username | opnum | date |
---|---|---|---|
1 | ssy | 0 | 20150424 |
如果执行了:
INSERT INTO count(username,opnum,date)
VALUES('ssy',5,'20150424') ON DUPLICATE KEY UPDATE opnum = opnum + 1;
结果为:
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;
结果为:
userid | username | opnum | date |
---|---|---|---|
1 | ssy | 1 | 20150424 |
2 | ssy | 10 | 20150424 |
3 | dlm | 10 | 20150424 |
这样,如果用户ssy在2015年4月24日这一天如果有操作,就追加操作数,如果该用户今天尚无操作,则增加一条今天的操作记录。即实现了多个用户的每日统计。
边栏推荐
- DDR4硬件原理图设计详解
- Guys, I tested three threads to write to three MySQL tables at the same time. Each thread writes 100000 pieces of data respectively, using F
- [JS learning notes 54] BFC mode
- Basic part - basic project analysis
- Bracket matching problem (STL)
- OneForAll安装使用
- How can edge computing be combined with the Internet of things?
- 购买小间距LED显示屏的三个建议
- Cron表达式(七子表达式)
- Beego cross domain problem solution - successful trial
猜你喜欢
Basic testing process of CSDN Software Testing Introduction
How to close the log window in vray5.2
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
Evolution of multi-objective sorting model for classified tab commodity flow
2022 Pengcheng cup Web
DDR4硬件原理图设计详解
2022 mobile crane driver examination question bank and simulation examination
如何将 DevSecOps 引入企业?
华为设备配置信道切换业务不中断
Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022
随机推荐
Home office things community essay
四部门:从即日起至10月底开展燃气安全“百日行动”
居家办公那些事|社区征文
Redis如何实现多可用区?
购买小间距LED显示屏的三个建议
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Characteristics and electrical parameters of DDR4
Three suggestions for purchasing small spacing LED display
sklearn模型整理
[JS learning notes 54] BFC mode
Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template
Question and answer 45: application of performance probe monitoring principle node JS probe
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
如何让全彩LED显示屏更加节能环保
I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
Harbor image warehouse construction
ZCMU--1390: 队列问题(1)
BOM//
[first release in the whole network] (tips for big tables) sometimes it takes only 1 minute for 2 hours of SQL operation
How to introduce devsecops into enterprises?