64

I am trying to select the last 6 months of entries in a table, I have a column called datetime and this is in a datetime mysql format.

I have seen many ways using interval and other methods - which method should I use? Thanks

1
  • 3
    if the field is really called datetime you'll need to use ` to surround it. datetime is a reserved word. Pablo's response would work xcept where he has your_dt_field you may need to put datetime with the proper escape ` character.
    – xQbert
    Jan 20, 2012 at 12:27

6 Answers 6

164

Use DATE_SUB

 .... where yourdate_column > DATE_SUB(now(), INTERVAL 6 MONTH)
2
  • DATE_SUB() function subtracts the interval from the given date. In this case "now() - 6 months".
    – prageeth
    Jul 11, 2012 at 13:08
  • Inside DATE_SUB() function, Can i also use MAX(<date_column>) instead of now()??? Because i'm facing a similar situation and i have to check the last 6 months data from the last date present in the date column instead of today's date.
    – LearneR
    Nov 17, 2015 at 0:45
14

Try this:

select *
  from table 
 where your_dt_field >= date_sub(now(), interval 6 month);

Query reads: give me all entries in table where the field corresponding to the entry date is newer than 6 months.

1
  • your_dt_field what will be it's format ? also how can use there systems current date Jul 5, 2016 at 11:35
5

You can get last six month's data by subtracting interval of 6 month from CURDATE() (CURDATE() is MySQL function which returns Today's date).

SELECT * FROM table 
         WHERE your_date_field >= CURDATE() - INTERVAL 6 MONTH;

Or you can use BETWEEN operator of MySQL as Below:

SELECT * FROM table 
         WHERE your_date_field BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE();
2

I tried @user319198 answer to display last 6 months (sum of) sales, it worked but I faced one issue in the oldest month, i do not get the sales amount of the whole month. The result starts from the equivalent current day of that month.

Just I want to share my solution if any one interested:-

yourdate_column > DATE_SUB(now(), INTERVAL 7 MONTH)
limit 6

Also it will be great if anyone has better solution for my case J.

2

You can also use TIMESTAMPDIFF

    TIMESTAMPDIFF(MONTH, your_date_column, now()) <= 6 )
1

To me, this looks like a solution as I'm using it with MariaDB, take a look at WHERE clause:

SELECT MONTH(yourTimestampOrDateColumn) AS MONTH, USER, ActionID, COUNT(*) AS TOTAL_ACTIONS, ROUND(SUM(totalpoints)) AS TOTAL_PTS
FROM MyTable
WHERE MONTH(yourTimestampOrDateColumn) BETWEEN MONTH(CURDATE() - INTERVAL 6 MONTH) AND MONTH(CURDATE())
GROUP BY MONTH;

queryResults

On the image we see only months where user had actual data recorded in a DB (thus showing only 4 months instead of 6).

So this month is 10th (October), 6 months ago was 4th month (April), thus query will look for that interval (from 4 to 10).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.