如何将组函数与 ORDER BY 子句配合使用?
我们可以利用 ORDER BY 子句中的组函数对结果集组进行排序。默认情况下,排序顺序是升序,但我们可以使用 DESC 关键字来反转它。
示例
mysql> Select designation, YEAR(Doj), count(*) from employees GROUP BY designation, YEAR(DoJ) ORDER BY Count(*) DESC; +-------------+-----------+----------+ | designation | YEAR(Doj) | count(*) | +-------------+-----------+----------+ | Prof | 2009 | 2 | | Asst.Prof | 2015 | 1 | | Asst.Prof | 2016 | 1 | | Prof | 2010 | 1 | | Asso.Prof | 2013 | 1 | +-------------+-----------+----------+ 5 rows in set (0.00 sec) mysql> Select designation, YEAR(Doj), count(*) from employees GROUP BY designation, YEAR(DoJ) ORDER BY designation DESC; +-------------+-----------+----------+ | designation | YEAR(Doj) | count(*) | +-------------+-----------+----------+ | Prof | 2009 | 2 | | Prof | 2010 | 1 | | Asst.Prof | 2015 | 1 | | Asst.Prof | 2016 | 1 | | Asso.Prof | 2013 | 1 | +-------------+-----------+----------+ 5 rows in set (0.00 sec)
广告