Forum Moderators: coopster

Message Too Old, No Replies

How to select items by months from mySQL database?

         

irock

4:09 pm on Feb 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I have a bunch of rows with release date in this format (2001-12-01) *no brackets obviously*

I would like to which mysql query & associated PHP codes to use if I like to list out the most recent 5 month of items in individual table seperated by months.

Here's the output I am lookin' for...

FEBRUARY 2004
----------------
x1 x2 x3
x4 x5 x6

JANUARY 2004
----------------
x1 x2 x3
x4 x5 x6

DECEMBER 2003
----------------
x1 x2 x3
x4 x5 x6

NOVEMBER 2003
----------------
x1 x2 x3
x4 x5 x6

OCTOBER 2003
----------------
x1 x2 x3
x4 x5 x6

I think I mostly need help on selecting by release month and seperating tables by months.

Thank much! You guys ROCK!

coopster

4:50 pm on Feb 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could use MySQL's MONTH [mysql.com] and MONTHNAME [mysql.com] functions:

SELECT
MONTH(my_month_column) as my_month,
MONTHNAME(my_month_column) my_monthname
FROM table
WHERE MONTH(my_month_column) IN ($last_five_months)
;

You would just need to build the $last_five_months variable as a comma-separated list.