当前位置:网站首页>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 .
边栏推荐
- Oneforall installation and use
- Stop saying that microservices can solve all problems!
- 数据库三大范式
- Intelligent metal detector based on openharmony
- [SWT component] content scrolledcomposite
- Manage multiple instagram accounts and share anti Association tips
- 龙蜥社区第九次运营委员会会议顺利召开
- COMSOL--三维随便画--扫掠
- 阻止浏览器后退操作
- uboot的启动流程:
猜你喜欢

基础篇——REST风格开发

Wechat nucleic acid detection appointment applet system graduation design completion (6) opening defense ppt

【爬虫】wasm遇到的bug

中非 钻石副石怎么镶嵌,才能既安全又好看?

如何将 DevSecOps 引入企业?

comsol--三维图形随便画----回转

2022 Pengcheng cup Web

COMSOL -- 3D casual painting -- sweeping

Wechat nucleic acid detection appointment applet system graduation design completion (7) Interim inspection report

How to introduce devsecops into enterprises?
随机推荐
不要再说微服务可以解决一切问题了!
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
7.2 daily study 4
[JS learning notes 54] BFC mode
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
C#实现WinForm DataGridView控件支持叠加数据绑定
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
TSQL – identity column, guid, sequence
POJ 3176-Cow Bowling(DP||记忆化搜索)
基础篇——基础项目解析
PHP中Array的hash函数实现
技术分享 | 常见接口协议解析
Ddrx addressing principle
中非 钻石副石怎么镶嵌,才能既安全又好看?
[TCP] TCP connection status JSON output on the server
Codeforces Round #804 (Div. 2)
go语言学习笔记-初识Go语言
How does redis implement multiple zones?
Intelligent metal detector based on openharmony
sklearn模型整理