当前位置:网站首页>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日这一天如果有操作,就追加操作数,如果该用户今天尚无操作,则增加一条今天的操作记录。即实现了多个用户的每日统计。
边栏推荐
- uboot的启动流程:
- Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- 龙蜥社区第九次运营委员会会议顺利召开
- 7.2每日学习4
- Pytorch training process was interrupted
- msfconsole命令大全,以及使用说明
- FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
- Paradigm in database: first paradigm, second paradigm, third paradigm
- Function///
猜你喜欢

LSTM applied to MNIST dataset classification (compared with CNN)

Wechat nucleic acid detection appointment applet system graduation design completion (8) graduation design thesis template

【Oracle】使用DataGrip连接Oracle数据库

DDRx寻址原理

Intelligent metal detector based on openharmony

go语言学习笔记-分析第一个程序

如何让全彩LED显示屏更加节能环保

AutoCAD -- mask command, how to use CAD to locally enlarge drawings

华为设备配置信道切换业务不中断

紫光展锐全球首个5G R17 IoT NTN卫星物联网上星实测完成
随机推荐
[there may be no default font]warning: imagettfbbox() [function.imagettfbbox]: invalid font filename
7.2每日学习4
Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022
如何让全彩LED显示屏更加节能环保
Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt
shell脚本文件遍历 str转数组 字符串拼接
技术分享 | 常见接口协议解析
About the use of Vray 5.2 (self research notes) (II)
Technology sharing | common interface protocol analysis
分类TAB商品流多目标排序模型的演进
我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
2022 mobile crane driver examination question bank and simulation examination
Ddrx addressing principle
go语言学习笔记-分析第一个程序
使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
COMSOL--三维随便画--扫掠
MFC pet store information management system
【广告系统】增量训练 & 特征准入/特征淘汰
Characteristics and electrical parameters of DDR4