I have an assignment
table which holds assignments created by a teacher in a particular class.
The assignment
table has the following fields:
idAssignment (primary key),
classId (foreign key),
title,
assignmentDesc,
dateDue
I also have an assignmentsubmissions
table where students in that class upload their submissions to the assignment.
The assignmentsubmission
table has the following fields:
assignmentSubmissionId (primary key),
assignmentId (foreign key),
userId (foreign key),
submission
Basically I want to join assignmentsubmission
to assignment
for a student in a particular class to see the assignments they have completed and not completed.
I have tried a number of different methods but this is my current SQL command:
SELECT *
FROM assignment
LEFT JOIN assignmentsubmission
ON assignment.idAssignment = assignmentsubmission.assignmentId
WHERE classId = 1234
AND ((userId = 1) OR (userId IS NULL))
The student mightn't have yet uploaded a submission to a particular assignment so in that case the userId
will not exist for that assignment
and I want to join null
rows for assignmentsubmission
. As it stands all that is happening is only the assignments which have been submitted are showing but I want all assignments, with a submission or null submission fields to be returned.
I'm really struggling with this problem so any help would be greatly appreciated, thanks.
You are on the right track, but the comparison to the student should be in the ON
clause:
SELECT a.*, (asu.userId IS NOT NULL) as completed_flag
FROM assignment a LEFT JOIN
assignmentsubmission asu
ON a.idAssignment = asu.assignmentId AND
asu.userId = 1
WHERE a.classId = 1234 ;
This version introduces table aliases which make the query easier to write and to read. Note that all column references are qualified -- meaning the table of origin is identified.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.