Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何编写查询以获取基于月份的数据?_Mysql_Sql - Fatal编程技术网

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。出席情况也一样=‘缺席’