select s.sex,count(s.sex) from student s GROUP BY sex;
GROUP BY 语句
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
测试student表纪录如下图,根据自己需求增删字段。
统计男女人数sql如下图:
student s ,s是自己为student表定义的别名,count()为统计的人数。
拓展资料:
SQL GROUP BY 语法:
SELECT column_name(列名), aggregate_function(column_name) (函数名) FROM table_name(表名) WHERE column_name operator value GROUP BY column_name
select sum(case when 性别='男' then 1 else 0 end) as 男人数,
sum(case when 性别='女' then 1 else 0 end) as 女人数 from student
select 性别,count(性别) from student group by 性别
select sex,count(sex) from student group by sex
结果如下
-----------------
sex Expr1
男 ....
女 ....
select sex,count(*) from student group by sex