Returning records from the last 3 months only in MySQL

后端 未结 4 1350
野性不改
野性不改 2020-12-01 07:35

I have a table with a timestamp field. How do I get data from the last 3 months?

In particular, March is my current month let say, 03/2012. I need to retur

4条回答
  •  日久生厌
    2020-12-01 08:32

    I know this is an old question, but to possibly save others time and to sum the above answers up for the case of needing (1) dates from current month and (2) dates from the prior 2 months (common when displaying data statistics):

    WHERE ((timestamp >= NOW() - DATE_FORMAT(CURDATE(), '%Y-%m-01'))
    OR  (timestamp >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH))
    

提交回复
热议问题