当前位置:Gxlcms > 数据库问题 > MySQL查询之分组查询

MySQL查询之分组查询

时间:2021-07-01 10:21:17 帮助过:16人阅读

列1,列2,聚合... from 表名 group by 列1,列2,列3...

查询男女生总数

select gender as 性别,count(*)
from students
group by gender;

查询各城市人数

select hometown as 家乡,count(*)
from students
group by hometown;

二、分组后的数据筛选

语法如下:

select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...

having后面的条件运算符与where的相同

查询男生总人数

方案一
select count(*)
from students
where gender=1;
-----------------------------------
方案二:
select gender as 性别,count(*)
from students
group by gender
having gender=1;

三、对比where与having

where是对from后面指定的表进行数据筛选,属于对原始数据的筛选

having是对group by的结果进行筛选

MySQL查询之分组查询

标签:sele   现在   mysql查询   nts   无法   运算符   指定   --   where   

人气教程排行