Mysql 如何编写查询以获取基于月份的数据?
我的数据库表是::Mysql 如何编写查询以获取基于月份的数据?,mysql,sql,Mysql,Sql,我的数据库表是:: attendence date admission number attendence 2013-10-2 LSTM-0008/2013-2014 present 2013-10-19 LSTM-0008/2013-2014 absent 2013-9-20 LSTM-0008/2013-2014 present 上面一个是我的数据库表 我想根据数据库表显示
attendence date admission number attendence
2013-10-2 LSTM-0008/2013-2014 present
2013-10-19 LSTM-0008/2013-2014 absent
2013-9-20 LSTM-0008/2013-2014 present
上面一个是我的数据库表
我想根据数据库表显示如下表:
MonthName totalWorkingDays Present absent
october 26 1 1
november 26 1 0
我这样编写mysql查询:
SELECT DISTINCT monthname(attendencedate)as monthname , COUNT (*) as totalworking days,
(SELECT COUNT(*) FROM lstms_attendence WHERE attendence='present' AND addmissionno='LSTM-0008/2013-2014') as present,
(SELECT COUNT(*) FROM lstms_attendence WHERE attendence='absent' AND addmissionno='LSTM-0008/2013-2014') as absent
FROM lstms_attendence
WHERE addmissionno='LSTM-0008/2013-2014'
GROUP BY attendencedate;
这对我不起作用。有人给我建议。试试这个:
SELECT monthname(attendencedate) AS monthname,
COUNT(*) AS totalworking_days,
SUM(CASE WHEN attendence = 'present' THEN 1 ELSE 0 END) AS present,
SUM(CASE WHEN attendence = 'absent' THEN 1 ELSE 0 END AS absent
FROM lstms_attendence
WHERE addmissionno = 'LSTM-0008/2013-2014'
GROUP BY monthname(attendencedate);
对于当前列中Attentience='present'的每一行,其总和为1,否则为0。出席情况也一样=‘缺席’