使用MySQL。 下面是我的表结构。
batch_admissions
------------------------+
batchId | studentId |
----------------------- +
1 | 1 |
1 | 2 |
1 | 3 |
2 | 1 |
2 | 2 |
------------------------+
attendance_master
----------------------------+
attendance | studentId |
----------------------------+
P | 1 |
P | 2 |
P | 3 |
----------------------------+
如果batchId=2
如下所示,则需要结果,因为attendance_master只包含batchId=1
的记录
----------------------------+
attendance | studentId |
----------------------------+
| 1 |
| 2 |
----------------------------+
但是目前我正在从attendance_master获取所有记录,而不管batchId的变化如何。 我的查询有什么问题?我认为左联盟应该做的工作。但没有工作
SELECT
a.attendanceId,
a.attendanceDate,
a.attendance,
a.Remarks,
CONCAT(b.studentFirstName, ' ', COALESCE(b.studentMiddleName,'') , ' ', b.studentLastName) as studentName ,
c.classRollNum,
SUBSTRING_INDEX(SUBSTRING_INDEX( a.attendanceDate , '-', 3 ),'-',-1) AS attDay,
CASE WHEN DAYNAME(a.attendanceDate) = 'Monday' THEN 'Mon'
WHEN DAYNAME(a.attendanceDate) = 'Tuesday' THEN 'Tue'
WHEN DAYNAME(a.attendanceDate) = 'Wednesday' THEN 'Wed'
WHEN DAYNAME(a.attendanceDate) = 'Thursday' THEN 'Thu'
WHEN DAYNAME(a.attendanceDate) = 'Friday' THEN 'Fri'
WHEN DAYNAME(a.attendanceDate) = 'Saturday' THEN 'Sat'
WHEN DAYNAME(a.attendanceDate) = 'Sunday' THEN 'Sun'
END as attDayName
,CONCAT(SUBSTRING_INDEX(SUBSTRING_INDEX( a.attendanceDate , '-', 3 ),'-',-1),'.',c.classRollNum) as Idx
FROM attendance_master a
LEFT JOIN student_master b ON a.studentId = b.studentId
LEFT JOIN batch_admissions c ON c.studentId = a.studentId AND c.batchId=1
WHERE a.attendanceDate BETWEEN '2016-03-01' AND '2016-03-31'
ORDER BY c.classRollNum ASC
-------------
基本上我试图避免触发两个查询并希望在单个查询中得到结果。
batch_admissions
表包含一系列批次,其中包含N个数学生。
attendance_master
表保留了所有批次学生的出勤率。
在网页上,我每批显示表格网格报告。
我想要实现的目标,
case 1
:当attendance_master不包含特定时段的batchId出勤时。仍然想要该批次的学生名单
-------------------------------------------------------
BatchId |studentId | Mon | Tue | Wed | Thus |
------------------------------------------------------
1 | 11 | | | | |
1 | 12 | | | | |
.. | .. | | | | |
Case 2
:当attendance_master包含特定时段的batchId出勤时。
-------------------------------------------------------
BatchId |studentId | Mon | Tue | Wed | Thus |
------------------------------------------------------
2 | 1 | P | P | P | P |
2 | 2 | P | A | P | P |
.. | .. | P | P | P | P |
Alternate我可以触发两个查询来实现逻辑上的这个。一个用于批量学生,然后获得所有学生的出勤细节。
答案 0 :(得分:1)
ok ...所以返回批量录取的所有记录和相关的student_master数据(总会有记录)和相关的考勤主数据......
FROM batch_admissions c
INNER JOIN student_master b
ON a.studentId = c.studentId
LEFT JOIN attendance_master a
ON c.studentId = a.studentId
and a.attendanceDate BETWEEN '2016-03-01' AND '2016-03-31'
WHERE c.batchId=1
ORDER BY c.classRollNum ASC