当前位置:Gxlcms > 数据库问题 > mysql统计同一个表中的不同条件下的个数

mysql统计同一个表中的不同条件下的个数

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

想要同时统计男生数量和不及格数量。
SELECT COUNT(1) AS boyNum FROM t_student WHERE sex=‘男‘;

SELECT COUNT(1) AS poorNum FROM t_student WHERE score<‘60‘;

失败的尝试:
SELECT COUNT(sex=‘男‘) AS boyNum, COUNT(score<‘60‘) AS poorNum FROM t_student;

解决方法:
mysql提供if函数,可以在查询是使用。
SELECT
SUM(
IF((sex=‘男‘),1,0)
) ‘boyNum’,
SUM(
IF((score<‘60‘),1,0)
) ‘poorNum’
FROM t_student;

mysql统计同一个表中的不同条件下的个数

标签:函数   个数   解决方法   from   方法   rom   sel   统计   core   

人气教程排行