Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mysql date extract with distinct

in mysql table i have the field 'date' which has the data as

2012-08-02 02:33:26
2012-08-02 05:33:26
2012-07-02 06:33:26
2012-06-02 01:33:26 
2012-06-10 09:33:26 
2011-05-10 10:33:26 
2011-04-10 02:33:26 

like that

i want to get the output as

2012-08
2012-07
2012-06
2011-05
2011-04

ie get the unique year and month (distinct of year and month of the date field)

I used the following query

SELECT  distinct(YEAR(date)) FROM table

It returns the year only. i want to get the output as defined above. is there any possibility to get the out put like this. How do i write the select query.

Please help to this.

like image 467
Vasanthan.R.P Avatar asked Dec 01 '22 21:12

Vasanthan.R.P


1 Answers

You might want to try this:

SELECT DISTINCT DATE_FORMAT(colName, '%Y-%m')
FROM tableName

SEE HERE: Date_Format( )

like image 67
John Woo Avatar answered Dec 21 '22 14:12

John Woo