0
SELECT
  Student.id,
  Student.first_name,
  Student.last_name, 
  count(`Attendance`.`id`) as total 
FROM `karate`.`attendances` AS `Attendance` 
RIGHT JOIN `karate`.`students` AS `Student` ON (`Attendance`.`student_id` = `Student`.`id`)
WHERE 1
  AND `Attendance`.`attendance_date` BETWEEN '2012-07-01' AND '2012-07-31' 
  AND (Student.active = '1') 
  AND (Student.type = 'student') 
GROUP BY `Student`.`id` 
ORDER BY total ASC

我希望所有学生记录在给定的日期范围内有出勤总数,但它只显示有出勤的学生的两个记录。但如果我们删除日期检查条件,它工作正常。

提前致谢

4

1 回答 1

0

这是因为attendance_date对于右连接的行,其值为 NULL。您需要在您的情况中包含此案例:

WHERE 1
  AND (`Attendance`.`attendance_date` IS NULL
         OR `Attendance`.`attendance_date` BETWEEN '2012-07-01' AND '2012-07-31')
  AND (Student.active = '1') 
  AND (Student.type = 'student')
于 2012-07-27T08:26:51.487 回答