views:

51

answers:

4

Hello friends!

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!

Edit: Thanks so much guys (or gals)!

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

+3  A: 

You're looking for a LEFT JOIN instead 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?

SELECT u.firstname, q.question, r.tutor_id, r.response, u2.firstname AS TutorName FROM response r 
LEFT JOIN question q ON q.id = r.question_id
JOIN user u ON u.id = q.user_id
JOIN user u2 on u2.id = r.tutor_id
Mike M.
No, I just have a user table that has a "user type" that distinguishes between basic users and tutors.
TBell
Is that Tutor_Id a value in the Users table? It's possible the User table holds the names of both tutors and survey takers. This is just a guess though!
Mike M.
tutor_id is the foreign key that links the response table to the user table. Basically a basic user can create a question, a response is linked to the question, and a tutor is linked to the response.
TBell
See my edit. You need a second join on the users table (alias of u2 this time) to retrieve the tutor's name.
Mike M.
Worked perfectly for pulling the tutors name, but still only displaying questions that have responses and not both. I appreciate the help so much!
TBell
You chose this answer, but did it give you the questions with no responses? If it didn't see what happens when you use FROM questions instead of FROM responses (see my answer - I think we were both on the same track).
Tim
I got the answer posted in my edit from a mixture of all the responses I got, haha. Appreciate the help!
TBell
Glad to help, This is why I love SO. I get the same help when needed :o)
Tim
+1  A: 

An INNER JOIN will cull out any non joining data. Use a LEFT JOIN instead, and you will get all of the rows from the "left hand side".

SELECT 
    u.firstname, 
    q.question, 
    r.tutor_id, 
    r.response 
FROM 
    question q          // left hand side

    INNER JOIN user u       // assumes all questions have a user
    ON u.id = q.user_id

    LEFT JOIN  response r 
    ON q.id = r.question_id 

Where is the tutor name stored?

OK, I'm not sure why some of the other solutions aren't quite working for you, but you can always do something like this to include tutor name:

SELECT 
        u.firstname, 
        q.question, 
        T.tutor_id,
        T.firstname, 
        T.response 
    FROM 
        question q          // left hand side

        INNER JOIN user u       // assumes all questions have a user
        ON u.id = q.user_id

        LEFT JOIN (
                SELECT
                    r.response,        
                    r.question_id,
                    r.tutor_id
                    u2.firstname
                FROM
                    response r 

                    INNER JOIN user u2
                    ON r.tutor_id = u2.id   // Is there a user type we can also join on?
            ) T 
        ON q.id = T.question_id 
ThatSteveGuy
Basic users and tutors are both stored in the user table and distinguished with a "user type". A basic user creates a question, a response is linked to that question, and a tutor (user) is linked to that response.
TBell
A: 
SELECT u.firstname, q.question, r.tutor_id, r.firstname as tutorname, r.response FROM 
response r RIGHT JOIN (question q INNER JOIN user u ON u.id = q.user_id)
ON q.id = r.question_id
Sadat
Another piece of the puzzle! However, it is displaying the same tutors name for every question, even if it has no response :(
TBell
A: 

Give this a shot. I don't know what tutor_id is joined to, but I assume it's the user id?

SELECT u.firstname, q.question, v.firstname, r.response FROM question q
LEFT JOIN q ON q.id = r.question_id
LEFT JOIN user u ON u.id = q.user_id
LEFT JOIN user v ON v.id = r.tutor_id

See that I grabbed the firstname of the user that answered (I think). I made that v (distinguishing it from the user u).

EDIT: I am kind of assuming that a "tutor" is just another user that answers the question.

Tim
Ah, pulling the tutors name but only displaying questions with responses and not both
TBell
It's late so I may have screwed something up ;o). Basically, I changed the answer to be FROM question rather than FROM response. That way we're looking for questions, not responses. Give me a few to go back and take a look at your question again.
Tim