Search code examples
phpmysqldatabasetimeline

Get years from articles


Table articles

id - content - ins_dt
1 - lorem ipsum1 - 2013-01-09
2 - lorem ipsum2 - 2013-02-08
3 - lorem ipsum3 - 2012-11-03
4 - lorem ipsum4 - 2011-01-20
5 - lorem ipsum5 - 2011-12-13
6 - lorem ipsum6 - 2010-05-12

With query

SELECT ins_dt FROM articles ORDER BY ins_dt DESC;

I get this: (http://sqlfiddle.com/#!2/fc9ea6/5)

"2013 2013 2012 2011 2011 2010" 

But I need this:

"2013 2012 2011 2010"

Solution

  • You can use simple SQL query

    SELECT GROUP_CONCAT(DISTINCT YEAR(ins_dt)) AS `year` 
    FROM articles 
    ORDER BY ins_dt DESC;
    

    Here is the sample and demo: http://sqlfiddle.com/#!2/fc9ea6/3