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
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?

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.