select 配合group by子句

2019-07-19  本文已影响0人  蔺蔺蔺赫赫

group by子句:配合聚合函数应用

                常用聚合函数

平均 AVG()

个数 count()

总和 SUM()

最大 MAX()

最小 MIN()

集合 GROUP_CONCAT()

例子1:统计每个国家的总人口

select countrycode,SUM(population) from city group by countrycode;

例子2:统计每个国家的城市个数

select countrycode,count(id) from city group by countrycode;

从city 这个表中 以国家为基准 统计国家城市的个数

1. 拿什么为基准

                      GROUP BY  countrycode

                2. 统计什么东西

                      城市id,name

                3. 统计东西的种类是什么?

                      COUNT(id)

例子3:统计每个国家的省名字列表

SELECT countrycode,GROUP_CONCAT(district) 

FROM city

GROUP BY countrycode;

从city 这个表中 以国家为基准 统计国家 和 城市名称的统计

例子4:统计中国每个省的城市名列表

select District,GROUP_CONCAT(NAME)

from city

where countrycode='CHN'

GROUP BY district;

从city  这个表中  以城市为基准  统计城市和名称 关于中国的

例子5:统计中国每个省的总人口数

select district,SUM(population)

from city

where countrycode='CHN'

GROUP BY district;

从city  这个表中  以城市为基准 统计城市和人口总数 关于中国的

上一篇 下一篇

猜你喜欢

热点阅读