一、DateDiff

    DateDIFF() 函数返回两个日期之间的天数

    语法:

    DATEDIFF(date1,date2)

    例子:

    1. SELECT DATEDIFF('2008-12-30','2008-12-29') AS DiffDate

    image.png
    (1)

    1. SELECT w2.Id
    2. FROM Weather w1, Weather w2
    3. WHERE DATEDIFF(w2.RecordDate, w1.RecordDate) = 1
    4. AND w1.Temperature < w2.Temperature

    image.png

    (2)
    image.png

    1. 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. (1) order_date between '2020-02-01' and '2020-02-29'
    2. (2) order_date like '2020-02%'
    3. (3) DATE_FORMAT(order_date, "%Y-%m") = "2020-02"
    4. (4) LEFT(order_date, 7) substr(1, 7)