Jump to content

[SOLVED] query last 6 months


borabora12

Recommended Posts

I want to show the last 6 months like this:

 

Jul

Aug

Sep

Oct

Nov

Dec

 

I already have this:

SELECT date, date_FORMAT(date, '%b') as 6thmonth FROM the_table ORDER BY id DESC LIMIT 1

 

Which gets the last date entered into the table. I was wondering how I could make either mysql or PHP find out the other 5 months. Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/136992-solved-query-last-6-months/
Share on other sites

Can you clarify a little?  Are you just wanting it to pull the previous 5 months from whatever month you pulled out of the table?  Or the previous 5 months that an event happened?  Do you have any code of what you are trying to accomplish?

 

Without knowing exactly the purpose you are trying to accomplish here, I will assume that you are trying to output the previous 5 months of the calendar year.  There are many ways that you could do this.  If they are listed in incremental order within this table, you could move your LIMIT 1 to LIMIT 6.  If that doesn't work, you could always create a new table with a first column that lists all of the months in the year, then the second column with their previous 5 months, or something to that nature.  Then, when you want to call those previous months, simply do a select on the table you just created like so:

 

SELECT month1, month2, month3, month4, month5 FROM table WHERE currentmonth = $variable

 

Am I even on the right track?

Are you just wanting it to pull the previous 5 months from whatever month you pulled out of the table?

YES!

 

The table itself contains weekday values. So if there is a way to do it that way it would be great, and if it can't just allow php to figure out the last 5 months based on what was extracted from the table,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.