bibabolynn / bibabolynn/learning-materials
group by having count
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
在介绍GROUP BY 和 HAVING 子句前,我们必需先讲讲sql语言中一种特殊的函数:聚合函数,
例如SUM, COUNT, MAX, AVG等。这些函数和其它函数的根本区别就是它们一般作用在多条记录上。
SELECT SUM(population) FROM bbc
这里的SUM作用在所有返回记录的population字段上,结果就是该查询只返回一个结果,即总人口数。
having是分组(group by)后的筛选条件,分组后的数据组内再筛选
where则是在分组前筛选
通过使用GROUP BY 子句,可以让SUM 和 COUNT 这些函数对属于一组的数据起作用。
当你指定 GROUP BY region 时, 属于同一个region(地区)的一组数据将只能返回一行值.
也就是说,表中所有除region(地区)外的字段,只能通过 SUM, COUNT等聚合函数运算后返回一个值.
select c.name, count(order_number) as count from orders o,customer c where c.id=o.customer_id group by customer_id;
+--------+-------+
| name | count |
+--------+-------+
| d | 9 |
| cc | 6 |
| 菩提子 | 1 |
| cccccc | 2 |
+--------+-------+
增加HAVING过滤
select c.name, count(order_number) as count from orders o,customer c where c.id=o.customer_id group by customer_id having count(order_number)>5;
+------+-------+
| name | count |
+------+-------+
| d | 9 |
| cc | 6 |
+------+-------+
from http://hejiajunsh.iteye.com/blog/1833847
Contributor guide
No contributing guide indexed for this repository
Research direction
Locate the repository's existing learning-materials structure and determine where this Chinese explanation belongs. Review the GROUP BY, HAVING, aggregate-function examples, and the cited source at hejiajunsh.iteye.com/blog/1833847. Done means the material has a clear location, consistent formatting, and accurately explains the shown query results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sql
- Domain
- databases, documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100