order by two columns
by Selva[ Edit ] 2010-02-02 14:49:14
Here is the issue and the solution which i have faced during a sql query where i want result like :
+-------+------+
| month | year |
+-------+------+
| 1 | 2010 |
| 12 | 2009 |
| 11 | 2009 |
| 10 | 2009 |
| 9 | 2009 |
| 8 | 2009 |
| 7 | 2009 |
+-------+------+
>> Consider the table name as stat and column name as month and year
>> First i have tried with the qurey : "select month,year from stat group by month, year order by month,year desc;"
mysql> select month,year from stat group by month, year order by month,year desc;
+-------+------+
| month | year |
+-------+------+
| 1 | 2010 |
| 7 | 2009 |
| 8 | 2009 |
| 9 | 2009 |
| 10 | 2009 |
| 11 | 2009 |
| 12 | 2009 |
+-------+------+
>> Next i have tried the right query : "select month,year from stat group by month order by year desc,month desc;";
mysql> select month,year from stat group by month order by year desc,month desc;
+-------+------+
| month | year |
+-------+------+
| 1 | 2010 |
| 12 | 2009 |
| 11 | 2009 |
| 10 | 2009 |
| 9 | 2009 |
| 8 | 2009 |
| 7 | 2009 |
+-------+------+
now i have got the desired result so if you want to order the result by consider two columns then try with qurey which is similar to "select month,year from stat group by month order by year desc,month desc;";