mysql使用where子句

时间:2018-10-01 15:57:45

标签: mysql

嗨,大家需要

方面的帮助

我如何计算带有where子句的另一个表中的行

所以我有两个桌子

我要计算学生的出勤表和出勤表

这是桌子上的学生

enter image description here

这里是餐桌席位

enter image description here

我尝试了这个,但是只是获取了出勤的学生

SELECT student.StudentID, student.`Name`, COUNT(attendance.AttendanceID) AS Total
FROM student
LEFT JOIN attendance ON student.StudentID = attendance.StudentID
where DateEntered between '2018-10-01' and '2018-10-01' 
GROUP BY student.StudentID,student.`Name`

我要帮助的是获取所有学生并计算他/她有多少次出勤次数。在日期输入处输入A

enter image description here

感谢您的帮助....

2 个答案:

答案 0 :(得分:0)

在左连接子句中放置条件:

SELECT student.StudentID, student.`Name`, COUNT(attendance.AttendanceID) AS Total
FROM student
LEFT JOIN attendance ON student.StudentID = attendance.StudentID
    and DateEntered between '2018-10-01' and '2018-10-01' 
GROUP BY student.StudentID,student.`Name`

答案 1 :(得分:0)

您可以让参加student的出勤率查询汇总:

SELECT     s.StudentID, 
           s.Name,
           COALESCE(Total, 0) 
FROM       student s
LEFT JOIN  (SELECT StudentId, COUNT(*) AS total
            FROM   attendance
            GROUP BY StudentId) ON s.StudentId = a.StudentId
WHERE      DateEntered BETWEEN '2018-10-01' and '2018-10-01'