0

如何显示缺席或当前结果

请帮我解决这个问题谢谢

现在显示这样的结果

________________________________________
 Name   TeacherNo   Attendance
________________________________________
 XYZ       993             5 Days Present

 Abc       991             7 Days Present

 123       955             2 Days Present

________________________________________

我想要这样

________________________________________________________________
Name    TeacherNo      Present Day          Absent Days
_________________________________________________________________
XYZ       993             5 Days Present        3 Days Present

Abc       991             7 Days Present        0 Days Present

123       955             2 Days Present        4 Days Present  

________________________________________________________________

这是代码

 $result1 = mysql_query("select name ,id ,`teacherno` as teacherno, CONCAT(count(`teacherno`),' Present days') as Attendance
   from tattendance 
   where `Attendance` = 'present' AND date BETWEEN '2013-04-01' AND '2013-04-07' group by `teacherno` order by attendance desc");
4

1 回答 1

1

您是否正在寻找类似以下查询的内容:

SELECT name, teacherno, 
    CONCAT(COALESCE(SUM(CASE WHEN Attendance = 'present' THEN 1 END),0),' Present days') as Present,
    CONCAT(COALESCE(SUM(CASE WHEN Attendance = 'absent' THEN 1 END),0),' Absent days') as Absent
FROM tattendance  
GROUP BY teacherno 

这用于根据出勤列将天数相加SUMCASE我还包括COALESCE返回 0 而不是NULL.

SQL 小提琴演示

于 2013-04-21T14:15:04.990 回答