Mysql查询使用两个条件对数据进行分组

时间:2018-04-20 23:36:58

标签: mysql sql

我有以下数据表,需要根据可用性和每个部门

完成学生计数的结果

enter image description here 注意:不同的部门可能会在以后进入。

enter image description here

想要得到如下结果:(学生按可用性和每个部门计算) enter image description here

2 个答案:

答案 0 :(得分:1)

首先,您需要在子查询上按department,studavailablity计数。然后使用GROUP_CONCAT函数通过子查询创建 studCount

如果您想在Yes前面显示No,可以在order by功能上添加GROUP_CONCAT

你可以试试这个。

SELECT department,GROUP_CONCAT(Concat(studavailablity,' ',c) order by 1 DESC)  'count'
FROM(
  SELECT count(1) c,department,studavailablity 
  FROM T
  GROUP BY studavailablity,department
) t
group by department
order by department desc

sqlfiddle:http://sqlfiddle.com/#!9/c4e56f/16

答案 1 :(得分:0)

SELECT Department, Studavailability, COUNT(*) FROM students GROUP BY Department, Studavailability;怎么样?祝你好运!