MySQL 如何在ORDER BY字句中使用group函数?

MySQL 如何在ORDER BY字句中使用group函数?

我们可以使用group函数在ORDER BY字句中按组对结果集进行排序。默认情况下,排序顺序为升序,但是我们可以使用DESC关键字将其反转。

阅读更多:MySQL 教程

示例

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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程