Returning data from a table by year

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Returning data from a table by year

Post by hairyjim »

Hi,

Any insight into the following would be good.

I have a MySQL table with data going back several years. I wish to retrieve the data from the table in year order but in recordsets of years & months.

So I wish to query the table and bring back the current year data, broken down by month and insert each month's records into an HTML table but each month to have its own table.

Jan 2009
record 1
record 2
record 3

Feb 2009
record 4
record 5
record 6
record 7

March 2009
record 8
record 9...and so on
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning data from a table by year

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Returning data from a table by year

Post by spider.nick »

This is belongs in the database section.

Anywho, I am not a MySQL guru. However, I would probably do it the following way:

Code: Select all

SELECT m, y, record_num FROM records ORDER BY m ASC, y ASC, record_num ASC
Then you would use PHP to manipulate the data into the tables.

Nick
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Re: Returning data from a table by year

Post by hairyjim »

VladSun wrote:That's called "grouping" :)

http://www.garfieldtech.com/blog/php-group-by

...thanks! I knew this technique had a name I just couldn;t figure out what it was! I have starting point thanks!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Returning data from a table by year

Post by VladSun »

spider.nick wrote:This is belongs in the database section.
IMHO, PHP-code is the appropriate section for this question.
There are 10 types of people in this world, those who understand binary and those who don't
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Returning data from a table by year

Post by spider.nick »

hairyjim wrote:
VladSun wrote:That's called "grouping" :)

http://www.garfieldtech.com/blog/php-group-by

...thanks! I knew this technique had a name I just couldn;t figure out what it was! I have starting point thanks!
From the posters question:
I have a MySQL table with data going back several years. I wish to retrieve the data from the table in year order but in recordsets of years & months.
Nick
Post Reply