0

Hi there I'm stuck on a problem with a personal website I'm building. The page is a user's dashboard that has a list of all the classes on the system. That table is called 'classes' and it is the parent table of 'class_reponses' which is a table that records if the user has either started, finished or failed to finish the class. 'class_responses' has a user_id in it. I'm trying to build a query that will list all of classes rows and simply have null fields when class_reponses user_id does not match the given id.

SELECT * FROM classes AS Class
LEFT JOIN class_responses AS ClassResponse
ON Class.id = ClassResponse.class_id
WHERE ClassResponse.user_id = 7

This query is something similar to what I'm trying to make, but if classes has no child class_responses that match the user_id then the classes row won't show up.

4

1 回答 1

1

将条件放在 ON 子句中的左连接表上。

SELECT * FROM classes AS c
LEFT JOIN class_responses AS cr
ON c.id = cr.class_id and cr.user_id = 7
于 2013-09-13T19:29:48.787 回答