Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Retrieving all the years in datetime field

Codes here !

Moderators: macek, egami, gesf

Retrieving all the years in datetime field

Postby MikeSpider » Thu Jun 21, 2012 6:07 pm

Hi guys,
My project stores user data in a db and i want to display this data dinamicaly to the user according
to his activity in particular years.

I need to select all the years recorded in db.

I tried "SELECT YEAR(date_column) FROM table_name" ;

not working .

PS: the dates are recorded on a datetime field type.

thanks in advance.
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Retrieving all the years in datetime field

Postby MikeSpider » Fri Jun 22, 2012 5:48 pm

ok i sorted it out.

"SELECT YEAR(date) AS year FROM tbl_name";
the problem now is that it gives me exactly all the years entered in db repeatedly.
it gives-me:
2009
2009
2009
2011
2012
2012

I just need one of each, which should be:
2009
2011
2012

I'm sorry if this seems like a beginner's question :S.
any help will be appreciated.

Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm

Re: Retrieving all the years in datetime field

Postby johnj » Fri Jun 22, 2012 7:39 pm

try select distinct.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Retrieving all the years in datetime field

Postby MikeSpider » Sat Jun 23, 2012 3:31 am

It works! Thanks a lot Johnj. :D
I was trying with the array_unique, but was only returning one date and not one of each.
Is good to learn something new everyday.

Cheers
Mike
MikeSpider
New php-forum User
New php-forum User
 
Posts: 49
Joined: Sun Feb 05, 2012 12:48 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 0 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.