mysql统计同一个表中的不同条件下的个数
时间:2017-08-07 18:24:51
收藏:0
阅读:456
想要同时统计男生数量和不及格数量。
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;
原文:http://www.cnblogs.com/rzjhxm/p/7300459.html
评论(0)