我的数据库表是:
attendence date admission number attendence
2013-10-2 LSTM-0008/2013-2014 present
2013-10-19 LSTM-0008/2013-2014 absent
2013-10-20 LSTM-0008/2013-2014 present
上面是我的数据库表。
我想在这里显示这样的表,totalworkingdays是基于数据库表的当前和不存在的月份的总和:
MonthName totalWorkingDays Present absent
october 3 2 1
我编写的mysql查询如下:
select distinct
monthname(attendencedate) as monthname,
count(*) as totalworkingdays
from lstms_attendence where
attendencedate> '2013-10-01' and attendencedate<'2013-10-31'
and addmissionno='LSTM-0008/2013-2014'
GROUP BY
monthname(attendencedate),
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);
我犯了如下错误:
错误代码:1064
您的SQL语法有错误;请检查对应于MySQL服务器版本的手册,以获取正确的语法,以便在第1行中使用“as present,SUM(CASE WHEN attentience='absent'THEN 1 ELSE 0 END)as absent FRO”
(0毫秒)
任何人都给我建议。提前谢谢。
最佳答案
记住总是以更可读的方式放置查询。
滚动的需要令人反感。
有点混乱
几个从句和分组从句。。。
看起来应该更像
SELECT DISTINCT
monthname(attendencedate) as monthname,
count(*) as totalworkingdays,
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
attendencedate> '2013-10-01' and attendencedate<'2013-10-31'
and addmissionno='LSTM-0008/2013-2014'
GROUP BY
monthname(attendencedate)
AFAIR您不能将
AS
放入GROUP BY子句中。这就是MySQL告诉你的。PS.未测试
关于java - 如何编写Mysql查询以基于日期从数据库获取数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20015627/