我需要一个月的名字和缺席人数。这是我的数据库查询:

SELECT sid,COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
      MONTHNAME(attendance_date) AS `Month_Name`
      FROM attendance
      WHERE SID = '2'
      AND campus_id = 2
      GROUP BY sid;

最佳答案

根据你的sid条款,分组没有意义-它总是'2'。相反,由于您希望按月计算名称,因此应该出现在where子句中:

SELECT   MONTHNAME(attendance_date) AS `Month_Name`,
         COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,
         COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
FROM     attendance
WHERE    sid = '2' AND campus_id = 2
GROUP BY MONTHNAME(attendance_date);

关于mysql - 需要缺席,并提供月份名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31177372/

10-16 13:19