从here中取得的示例,我试图了解GROUP BY
完全做了什么:
鉴于此employee
表:
+-------+----------+--------+------------+
| Empid | Empname | Salary | DOB |
+-------+----------+--------+------------+
| 1 | Habib | 2014 | 2004-12-02 |
| 2 | Karan | 4021 | 2003-04-11 |
| 3 | Samia | 22 | 2008-02-23 |
| 4 | Hui Ling | 25 | 2008-10-15 |
| 5 | Yumie | 29 | 1999-01-26 |
+-------+----------+--------+------------+
执行mysql> select * from employee group by empname;
我们得到:
+-------+----------+--------+------------+
| Empid | Empname | Salary | DOB |
+-------+----------+--------+------------+
| 1 | Habib | 2014 | 2004-12-02 |
| 4 | Hui Ling | 25 | 2008-10-15 |
| 2 | Karan | 4021 | 2003-04-11 |
| 3 | Samia | 22 | 2008-02-23 |
| 5 | Yumie | 29 | 1999-01-26 |
+-------+----------+--------+------------+
那么,这是否意味着GROUP BY只是按键对表进行排序?
谢谢
答案 0 :(得分:13)
GROUP BY启用摘要。具体来说,它控制使用COUNT(),SUM(),AVG(),MIN(),MAX()等汇总函数。在您的示例中没有太多要总结的内容。
但是,假设你有一个Deptname专栏。然后你可以发出这个查询并通过Deptname获得平均工资。
SELECT AVG(Salary) Average,
Deptname
FROM Employee
GROUP BY Deptname
ORDER BY Deptname
如果您希望按特定顺序放置结果集,请使用ORDER BY。