我正在为php和mysql中的学校创建考勤管理系统。我在数据库中有3个表:
1. Students
id
student_name
class
2. subjects
id
subjects
3. attendances
id
subject_id
student_id
attend (boolean)
today_date
我只存储在考勤表上上课的学生。
我想抓住所有参加或不参加特定班级,日期和科目的学生名单(参加或不参加考勤表)。
像;
Attendance for Class - X(Physics) on 18-08-2017
______________________________
| Student Name | Attendance |
------------------------------
| Foo | 0 |
| Bar | 1 |
| Baz | 0 |
| Qux | 1 |
------------------------------
我正在尝试查询
SELECT s.id,s.student_name, a.attend as attend
FROM attendances as a
INNER JOIN students as s on s.id = a.student_id
WHERE s.class = 'X'
AND a.subject_id = 3
AND a.date = '2017-08-18'
但我没有得到我想要的结果。我相信会有一些更好,更简单的方法来实现这一点,因为我之前没有以这种方式使用SQL。
答案 0 :(得分:1)
以下查询可能有助于您获得所需的结果:
SELECT s.id,s.student_name, IFNULL(a.attend, 0) as attend FROM students as s LEFT JOIN attendances as a on s.id = a.student_id WHERE s.class = 'X' AND a.subject_id = 3 AND a.date = '2017-08-18'
答案 1 :(得分:1)
SELECT s.id,s.student_name, a.attend as attend FROM attendances as a
RIGHT JOIN students as s on s.id = a.student_id AND a.subject_Id=3
AND a.date='2017-08-18' WHERE s.class = 'X'