一、DateDiff
DateDIFF() 函数返回两个日期之间的天数
语法:
DATEDIFF(date1,date2)
例子:
SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate
(1)
SELECT w2.Id
FROM Weather w1, Weather w2
WHERE DATEDIFF(w2.RecordDate, w1.RecordDate) = 1
AND w1.Temperature < w2.Temperature
(2)
select activity_date as day, count(distinct user_id) as active_users from Activity where dateDiff('2019-07-27',activity_date)<30 group by activity_date
二、指定日期的方法
看了一下评论,总结一下时间怎么指定2020年2月的方法:
(1) order_date between '2020-02-01' and '2020-02-29'
(2) order_date like '2020-02%'
(3) DATE_FORMAT(order_date, "%Y-%m") = "2020-02"
(4) LEFT(order_date, 7) 或 substr(1, 7)