I’m coming up stumped on forming a working SQL statement for a project of mine. First of all, I have a “questions” table and a “response” table. The response table is tied to the question table through a foreign id called “question_id” (tied to the questions “id”). A question doesn’t necessarily have to have a response, but the only working statement I can come up with will only pull a question that has a response, but I need it to show every question whether or not there is a response.
That SQL statement is:
SELECT u.firstname, q.question, r.tutor_id, r.response FROM response r
JOIN question q ON q.id = r.question_id
JOIN user u ON u.id = q.user_id
My other problem is that I’m also trying to pull the firstname of the tutor, but can only pull the “tutor_id”, so any help with that would also be awesome. If anyone has any tips, I’d appreciate it!
Works with:
SELECT u.firstname, q.question, v.firstname, r.response
FROM question q
INNER JOIN user u ON u.id = q.user_di
LEFT JOIN response r ON q.id = r.question_id
LEFT JOIN user v ON v.id = r.tutor_id
You’re looking for a
LEFT JOINinstead of an inner join. This will return all values on the first table regardless of whether or not it matches one from the second table.As for question two, it looks like you need a tutor table to join on to get the name. Does one exist?