当前位置:网站首页>[Mysql] DATEDIFF function
[Mysql] DATEDIFF function
2022-07-30 05:56:00 【iijik55】
The DATEDIFF function returns the number of days between two dates
Syntax format
DATEDIFF(date1,date2)Parameter description
date1: compare date 1
date2: compare date 2
The DATEDIFF function returns the calculation result of date1 - date2. The two parameters, date1 and date2, must be valid date or datetime values; if the parameters are passed datetime values, the DATEDIFF function only uses the date part for calculation and ignores it.Time part (only the date part of the value is involved in the calculation)
Example
-- 1SELECT DATEDIFF('2022-04-30','2022-04-29');-- 0SELECT DATEDIFF('2022-04-30','2022-04-30');-- -1SELECT DATEDIFF('2022-04-29','2022-04-30');-- 0SELECT DATEDIFF('2022-04-30','2022-04-30 14:00:00');-- 1SELECT DATEDIFF('2022-04-30 13:00:00','2022-04-29 14:00:00');-- 10SELECT DATEDIFF('2017-06-25 09:34:21', '2017-06-15');The difference between the DATEDIFF function and the TIMESTAMPDIFF function
1. The DATEDIFF function is only used to return the number of days between two dates, and the TIMESTAMPDIFF function is used to return and calculate the time difference between the specified units of two dates (the specified unit can be years, quarters, months, weeks, days, hours, minutes, secondsetc.)
2. The calculation method of the date difference is reversed
The syntax format of the DATEDIFF function: DATEDIFF(start,end)
DATEDIFF function returns the calculation result of start - end
The syntax format of the TIMESTAMPDIFF function: TIMESTAMPDIFF(DAY,start,end)
TIMESTAMPDIFF function returns the calculation result of end - start
-- -2SELECT DATEDIFF('2022-04-28', '2022-04-30');-- 2SELECT TIMESTAMPDIFF(DAY,'2022-04-28', '2022-04-30');3. If the time is in YYYY-MM-DD HH:MM:SS format, when calculating the difference in days, TIMESTAMPDIFF uses the 24-hour format, even if it is 23:59:59, it is not counted as a day, andDATEDIFF is the direct subtraction of the part of the intercepted date.Then the number of days calculated using the two functions is different at this time
-- -2SELECT DATEDIFF('2022-04-30 00:00:00','2022-04-28 23:59:59');-- 1SELECT TIMESTAMPDIFF(DAY,'2022-04-28 23:59:59', '2022-04-30 00:00:00');When the time format is YYYY-MM-DD HH:MM:SS, the results of using the two functions are different and need to be selected according to specific business scenarios
Exercise examples
Import data
DROP TABLE IF EXISTS coupon_collection;CREATE TABLE coupon_collection(user_id VARCHAR(8),collection_date DATE)ENGINE=InnoDBDEFAULT CHARSET = utf8;INSERT INTOcoupon_collection (user_id,collection_date)VALUE ('u001','2021-05-01'),('u002','2021-05-01'),('u003','2021-05-02'),('u004','2021-05-02'),('u005','2021-05-03');DROP TABLE IF EXISTS consumption_info;CREATE TABLE consumption_info(user_id VARCHAR(8),consumption_date DATE)ENGINE=InnoDBDEFAULT CHARSET = utf8;INSERT INTOconsumption_info (user_id, consumption_date)VALUE ('u002','2021-04-28'),('u001','2021-04-29'),('u001','2021-05-03'),('u003','2021-05-05'),('u005','2021-05-06'),('u001','2021-05-08'),('u004','2021-05-09'),('u006','2021-05-09'),('u003','2021-05-10'),('u002','2021-05-10');coupon_collection form (e-commerce coupon collection form)

user_id: User ID (VARCHAR type) collection_date: Date of coupon collection (DATE type)
consumption_info table (e-commerce consumption table)

user_id: User ID (VARCHAR type) consumption_date: Consumption date (DATE type)
Question: The coupons received by the user will take effect the next day, and will automatically take effect when shopping within the next 7 days, and the number of times of use is unlimited.It is required to obtain the users who have successfully used coupons to consume and their corresponding consumption times (output format: user_id, num)
user_id: The ID of the user who successfully used the coupon to consume num: The number of times of consumption
SELECT a.user_id, COUNT(collection_date)AS numFROM coupon_collection AS aINNER JOIN consumption_info AS bON a.user_id = b.user_idWHERE DATEDIFF(b.consumption_date,a.collection_date) BETWEEN 1 AND 7GROUP BY a.user_id;Result display:

Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
- 给小白的 PostgreSQL 容器化部署教程(上)
- 4461. 范围分区(Google Kickstart2022 Round C Problem B)
- 分布式事务之 Seata框架的原理和实战使用(三)
- MySQL索引从入门到深入学习
- [Redis Master Cultivation Road] Jedis - the basic use of Jedis
- curl (7) Failed connect to localhost8080; Connection refused
- 力扣20-有效的括号——栈实现
- MySQL夺命10问,你能坚持到第几问?
- 分布式事务之 Atomikos 原理和使用(一)
- MySQL kills 10 questions, how many questions can you stick to?
猜你喜欢

视野 | KeyDB:为 Web 应用而生的高性能 Redis 分支

MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)

翻译 | Kubernetes 将改变数据库的管理方式

2022鹏城杯web

分布式事务之 Seata框架的原理和实战使用(三)

cookie和session区别

MySQL(3)

mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")

从字节码角度带你彻底理解i++与++i

个人博客系统(附源码)
随机推荐
给小白的 PostgreSQL 容器化部署教程(上)
839. 模拟堆
解决没有配置本地nacos但是一直发生localhost8848连接异常的问题
五一去见了一些身价数千万的成功人士,我一些新的思路和启示
Within the SQL connection table (link connections, left or right, cross connection, full outer join)
The use of Conluce, an online document management system
力扣344-反转字符串——双指针法
Seata exception: endpoint format should like ip:port
JVM 垃圾回收 超详细学习笔记(二)
2022年SQL大厂高频实战面试题(详细解析)
号称年薪30万占比最多的专业,你知道是啥嘛?
mysql basics (4)
文档在线化管理系统Confluce使用
cnpm安装步骤
The Golden Circle Rule: Deep Thinking Methods for Successful People
Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind
破纪录者(Google Kickstart2020 Round D Problem A)
【Koltin Flow(一)】五种创建flow的方式
无代码开发平台重新申请入门教程
[GStreamer] 插件的名字要和GST_PLUGIN_DEFINE匹配