如何在数据库中的attendance_tbl中忽略出勤/缺席/延迟计算假期?

时间:2018-04-06 16:18:53

标签: php mysql

所以这是我的代码:

SELECT s.student_id as 'Student_Number', CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) 'Student_Name',s.student_program as 'Program', (SUM(IF(stat.status_description='Late',1,0))) 'Total_Lates', (SUM(IF(stat.status_description='Absent',1,0))) 'Total_Absences', Floor((SUM(IF(stat.status_description='Late',1,0))) / 3 + (SUM(IF(stat.status_description='Absent',1,0)))) 'Total_Absence_with_Lates' FROM attendance_tbl a LEFT JOIN student_tbl s ON s.student_id=a.entity_id LEFT JOIN status_tbl stat ON stat.status_id=a.status_id where a.course_id='$course' and a.entity_type='student' and CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) like '%$stname%' GROUP BY a.entity_id, CONCAT(IFNULL(s.student_fname,''),' ', IFNULL(s.student_mname,''),' ', IFNULL(s.student_lname,'')) order by Student_Name ASC

我正在计算每个学生的缺勤情况。 1缺席= 1缺席和3缺席= 1缺席。

以下是我的查询的输出:

enter image description here

这是我获取数据FROM的表:

ATTENDANCE_TBL:它的行是:

attendance_id | entity_id | course_id | rooms_id | status_id | date | time | term | entity_type
这张表基本上是学生的出勤率。状态id只是1-5的整数类型1表示存在,2 - 迟,3 - 表示,4 - 原谅,5 - 切割。

我也有STATUS_TBL:它的行是:status_id | status_description 状态ID是1-5,然后描述是1表示存在,2 - 迟,3 - 存在,4 - 原谅,5 - 切割。

STUDENT_TBL行:

student_id | studant_fname | studant_mname | studant_lname | student_program 

student_program是学生的课程,如旅游,创业等......

总的来说,这是我想要帮助的问题:我想忽略假期的假期。

我有holiday_table:

它有

announcement_id | announcement_date | announcement_description | term

如果announcement_date与attendance_tbl中的日期相同,并且holiday_table中的条款与attendance_tbl相同,则它将忽略当天的出勤/缺席/延迟计数。基本上它不会算上它

1 个答案:

答案 0 :(得分:0)

当您在announcement_date和term上对holiday_table进行左联接时,检查假日日期是否为空,那么您的原始查询将不计算那些被视为假期的日期。您没有向我们提供attendance_tbl的列,因此您可以在下面找出查询。

select <...>
FROM attendance_tbl a 
LEFT JOIN holiday_table h ON h.announcement_date = a.<date_column> and h.term = a.term
where h.announcement_date is null
and ...
Group By...
order by Student_Name ASC;