当前位置:网站首页>[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
边栏推荐
猜你喜欢

一文带你吃透js处理树状结构数据的增删改查

Concurrent Programming Review

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

MySQL安装配置教程(超级详细)

Within the SQL connection table (link connections, left or right, cross connection, full outer join)

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

解决没有配置本地nacos但是一直发生localhost8848连接异常的问题

mysql basics (4)

Programmers care guide, give yourself a chance to make the occasional relaxation of body and mind

MySql字符串拆分实现split功能(字段分割转列、转行)
随机推荐
翻译 | Kubernetes 将改变数据库的管理方式
MYSQL-InnoDB的线程模型
上交所行情文件解析之mktdt04
[Mysql] CONVERT函数
C language implements highly secure game archives and reads files
429. N 叉树的层序遍历(两种解法)
go版本升级
4、nerf(pytorch)
cookie和session区别
SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
108. 将有序数组转换为二叉搜索树
容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
1475. 商品折扣后的最终价格
MySQL安装配置教程(超级详细)
2022年比若依更香的开源项目
Thymeleaf简介
容器化|在 S3 备份恢复 RadonDB MySQL 集群数据
MySql string splitting realizes the split function (field splitting, column switching, row switching)
Oracle补丁体系及Opatch工具介绍