Page 1 of 1

Retrieving all the years in datetime field

Posted: Thu Jun 21, 2012 6:07 pm
by MikeSpider
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.

Re: Retrieving all the years in datetime field

Posted: Fri Jun 22, 2012 5:48 pm
by MikeSpider
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

Re: Retrieving all the years in datetime field

Posted: Fri Jun 22, 2012 7:39 pm
by johnj
try select distinct.

Re: Retrieving all the years in datetime field

Posted: Sat Jun 23, 2012 3:31 am
by MikeSpider
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