分组做的是什么? [英] What does group by do exactly ?

查看:127
本文介绍了分组做的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过此处的示例,我'试图了解 GROUP BY 完全是什么:



鉴于 employee 表:

  + ------- + --------- -  + -------- + ------------ + 
| Empid | Empname |薪水| DOB |
+ ------- + ---------- + -------- + ------------ +
| 1 | Habib | 2014 | 2004-12-02 |
| 2 |卡兰| 4021 | 2003-04-11 |
| 3 | Samia | 22 | 2008-02-23 |
| 4 |惠玲| 25 | 2008-10-15 |
| 5 | Yumie | 29 | 1999-01-26 |
+ ------- + ---------- + -------- + ------------ +

执行 mysql>后,通过empname选择* from employee group;



我们得到:

  + ------- + ---------- + -------- + ------------ + 
| Empid | Empname |薪水| DOB |
+ ------- + ---------- + -------- + ------------ +
| 1 | Habib | 2014 | 2004-12-02 |
| 4 |惠玲| 25 | 2008-10-15 |
| 2 |卡兰| 4021 | 2003-04-11 |
| 3 | Samia | 22 | 2008-02-23 |
| 5 | Yumie | 29 | 1999-01-26 |
+ ------- + ---------- + -------- + ------------ +

那么,这是否意味着GROUP BY只是通过键排序表?

感谢

解决方案

GROUP BY启用摘要。具体来说,它控制着像COUNT(),SUM(),AVG(),MIN(),MAX()等总结函数的使用。在你的例子中没有太多的总结。

但是,假设您有一个Deptname列。然后,您可以发出此查询并获得Deptname的平均工资。

 选择AVG(薪水)平均值,
Deptname
FROM员工
GROUP BY部门名称
ORDER BY部门名称

如果您希望将结果集放入特定顺序,请使用ORDER BY。

From an example taken from here , I'm trying to understand what does GROUP BY do exactly :

Given this employee table :

+-------+----------+--------+------------+
| 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 |
+-------+----------+--------+------------+

After executing mysql> select * from employee group by empname;

We get :

+-------+----------+--------+------------+
| 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 |
+-------+----------+--------+------------+

So , does that mean that GROUP BY just sorts a table by key ?

Thanks

解决方案

GROUP BY enables summaries. Specifically, it controls the use of summary functions like COUNT(), SUM(), AVG(), MIN(), MAX() etc. There isn't much to summarize in your example.

But, suppose you had a Deptname column. Then you could issue this query and get the average salary by Deptname.

SELECT AVG(Salary) Average,
       Deptname
  FROM Employee
 GROUP BY Deptname
 ORDER BY Deptname

If you want your result set put in a certain order, use ORDER BY.

这篇关于分组做的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆