Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select null on a join where a record doesn't exist in another table

I've got a problem that I can't seem to figure out after a bunch of failed attempts.

I've got three tables that I need to do a join on for some reporting, and in the 2nd table a record might not exist. If the record doesn't exist, I need to report a null value for the data that comes from the 3rd table.

In the most basic form, here are the table structures (it's for a survey)

Table A (Survey)
-----------------------
SurveyNumber | SurveyId
016243023708   1152010

Table B (Response)
----------------------------------
SurveyId | QuestionId | ResponseId
1152010    1279235      486

Table C (Response Values)
--------------------
ResponseId | Value
486          Yes

To explain why a record may not exist in Table B is simply because the values are inserted as the survey is completed. If the user leaves the survey without finishing (they can come back later), the record in Table B won't be there. The value in table C should be reported as null for this.

If it makes it any easier, I need to do the reporting specifically for questionid 1279235.

This is the query I've come up with so far (it shows me everything but the surveys with a missing record in table b for question 1279235).

SELECT      A.SurveyNumber, A.SurveyId, B.QuestionID, C.Value
FROM        tblA A
LEFT JOIN   tblB B
        ON  A.SurveyId = B.SurveyId
LEFT JOIN   tblC C
        ON  B.ResponseId = C.ResponseId 
WHERE       B.QuestionId = 1279235

I can provide more clarification if it is needed.

Thanks in advance

like image 409
jsmith Avatar asked Nov 21 '12 13:11

jsmith


People also ask

How do I select data from one table is not in another table?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

How do you handle NULL in join?

Null values in tables or views being joined never match each other. Since bit columns do not permit null values, a value of 0 appears in an outer join when there is no match for a bit column in the inner table. The result of a join of null with any other value is null.

Can you inner join a table that doesn't have matching records?

The JOIN or INNER JOIN does not return any non-matching rows at all. It returns only the rows that match in both of the tables you join. If you want to get any unmatched rows, you shouldn't use it. The LEFT JOIN and the RIGHT JOIN get you both matched and unmatched rows.

How do you select rows with no matching entry in another table?

1 Answer. Here, LEFT JOIN is used to return all the rows from TableA even though they don't match with the rows in TableB. You can observe that WHERE tb.ID IS NULL clause; there will be no records in TableB for the particular ID from TableA.


2 Answers

do not put the condition in the where clause, but instead on the join part since records on tablec may not exist.

SELECT      A.SurveyNumber, A.SurveyId, B.QuestionID, C.Value
FROM        tblA A
            LEFT JOIN   tblB B
                  ON  A.SurveyNumber = B.SurveyNumber AND 
                      B.QuestionId = 1279235
            LEFT JOIN   tblC C
                  ON  B.ResponseId = C.ResponseId
like image 91
John Woo Avatar answered Nov 15 '22 07:11

John Woo


try this:

SELECT
    A.SurveyNumber, A.SurveyId, B.QuestionID, C.Value
    FROM tblA        A
    LEFT JOIN tblB   B ON  A.SurveyId=B.SurveyId AND B.QuestionId=1279235
    LEFT JOIN tblC   C ON  B.ResponseId=C.ResponseId 

EDIT working example:

DECLARE @tblA  table (SurveyNumber varchar(12),SurveyId int)
INSERT INTO @tblA VALUES ('016243023708',   1152010)
INSERT INTO @tblA VALUES ('016243023708',   1152011)

DECLARE @tblB table (SurveyId int, QuestionId int, ResponseId int)
INSERT INTO @tblB values (1152010,    1279235,      486)
INSERT INTO @tblB values (1152011,    1279235,      487)

DECLARE @tblC  table (ResponseId int, ValueOf varchar(10))
INSERT INTO @tblC values (486,          'Yes')


SELECT
    A.SurveyNumber, A.SurveyId, B.QuestionID, C.ValueOf
    FROM @tblA        A
    LEFT JOIN @tblB   B ON  A.SurveyId=B.SurveyId AND B.QuestionId=1279235
    LEFT JOIN @tblC   C ON  B.ResponseId=C.ResponseId 

OUTPUT:

SurveyNumber SurveyId    QuestionID  ValueOf
------------ ----------- ----------- ----------
016243023708 1152010     1279235     Yes
016243023708 1152011     1279235     NULL

(2 row(s) affected)
like image 41
KM. Avatar answered Nov 15 '22 06:11

KM.