Working with INTERVAL and CURDATE in MySQL

后端 未结 3 1885
忘了有多久
忘了有多久 2020-12-01 05:13

I\'m building a chart and I want to receive data for each month.

Here\'s my first request which is working:

SELECT s.GSP_nom AS nom, timestamp, AVG         


        
相关标签:
3条回答
  • 2020-12-01 05:40

    I usually use

    DATE_ADD(CURDATE(), INTERVAL - 1 MONTH)
    

    Which is almost same as Pekka's but this way you can control your INTERVAL to be negative or positive...

    0 讨论(0)
  • 2020-12-01 05:52

    As suggested by A Star, I always use something along the lines of:

    DATE(NOW()) - INTERVAL 1 MONTH
    

    Similarly you can do:

    NOW() + INTERVAL 5 MINUTE
    "2013-01-01 00:00:00" + INTERVAL 10 DAY
    

    and so on. Much easier than typing DATE_ADD or DATE_SUB all the time :)!

    0 讨论(0)
  • 2020-12-01 05:53

    You need DATE_ADD/DATE_SUB:

    AND v.date > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
    AND v.date < (DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
    

    should work.

    0 讨论(0)
提交回复
热议问题