Returning records from the last 3 months only in MySQL

后端 未结 4 1351
野性不改
野性不改 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:26

    To get the first day of the current month, you could use this:

    DATE_FORMAT(CURDATE(), '%Y-%m-01')
    

    if current date is 2013-03-13, it will return 2013-03-01, and we can just substract 2 months from this date to obtain 2013-01-01. Your query could be like this:

    SELECT *
    FROM yourtable
    WHERE data >= DATE_FORMAT(CURDATE(), '%Y-%m-01') - INTERVAL 2 MONTH
    

提交回复
热议问题