如何从使用where条件获取计数值

时间:2016-05-12 06:58:16

标签: mysql group-by

select count(*) total, sum(case when stu_community = 'BC' then 1 else 0 
end) bc, b.branch_name FROM admission_details a, stu_personal_details p,
stu_contact_details c, current_course curr,degree d, courses co, branch 
b WHERE a.admission_no = p.admission_no AND p.stu_rollno = c.stu_rollno 
AND c.stu_rollno = curr.stu_rollno AND curr.stu_rollno = p.stu_rollno 
AND (curr.stu_branch IN (18,19,20) AND d.degree_id = curr.stu_degree AND
co.course_id = curr.stu_course AND b.branch_id = curr.stu_branch)

以上查询提供以下结果集
enter image description here

我想要来自每个分支的计数值和每个社区在同一个查询中它可以吗?我想要这样的结果enter image description here

我该怎么办?请帮忙

1 个答案:

答案 0 :(得分:0)

您需要使用group by函数来获得预期结果

在查询结尾处

添加

 group by b.branch_name