Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return null values using SQL Left Join

Please look at this SQL Fiddle for the tables and what query I have tried:
SQL Fiddle

So basically, I have two tables namely tbl_curriculum and tbl_enrolled_subjects.

tbl_curriculum contains all the subjects (subject_id) a student should take based on his course (course_id).
tbl_enrolled_subjects contains all the subjects the student has taken/enrolled based on tbl_curriculum.

I want to check which subjects the student has taken and which is not, the query should return something like this:

Subject_id|Grade|Status
23        | 2   |Passed
24        | 2   |Passed
31        | 2   |Passed
50        | 2   |Passed
83        | 1   |Passed
27        |NULL |NULL 
28        |NULL |NULL 
29        |NULL |NULL 

. . . And So On.

Subject_ID with Grade and Status mean the student has already taken the subject. On the otherhand, NULL values indicates the the student has not taken those subjects yet.
I used this query:

SELECT a.subject_id, b.grade, b.status
FROM tbl_curriculum a
LEFT JOIN tbl_enrolled_subjects b
ON a.course_id = b.course_id  AND a.subject_id = b.subject_id
WHERE b.student_id_no='05-0531';

But I keep getting only the subjects the student has taken.

Subject_id|Grade|Status
23        | 2   |Passed
24        | 2   |Passed
31        | 2   |Passed
50        | 2   |Passed
83        | 1   |Passed

Am I missing something? Thanks in advance.

like image 884
Ruben_PH Avatar asked Dec 10 '12 03:12

Ruben_PH


People also ask

Can LEFT join return NULL?

Left Join returns a null even when the match exists.

How get NULL values in SQL join?

The result of a join of null with any other value is null. Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.

Does join return NULL values?

When using left join, right join, full (outer) join, it is possible to return NULL value, while (inner) join, cross join will not return NUll value. The following example is the difference between these joins, please refer to: --join.

What does a LEFT join return if there are no matches?

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.


2 Answers

The reason you are not finding any nulls is because your where-clause is searching for rows with a student_id_no filled out. But, with the data you have the student_id_no will also be null in the case where the student has not taken the class...thus you are filtering out those.

Try this:

SELECT a.subject_id, b.grade, b.status, b.student_id_no
FROM tbl_curriculum a
LEFT JOIN tbl_enrolled_subjects b
ON a.course_id = b.course_id  AND a.subject_id = b.subject_id
where student_id_no is null or student_id_no = '05-0531'
order by subject_id

http://sqlfiddle.com/#!2/4c7b2/43

like image 200
purgatory101 Avatar answered Sep 22 '22 12:09

purgatory101


Is very simple, you use IS EMPTY

In your entity you have defined a inverse key, then you call your entity principal where inverse Key IS EMPTY.

SELECT a FROM tbl_curriculum a WHERE a.enrollers IS EMPTY;

Then I have a field curriculum defined like tbl_enrolled_subjects in the tbl_curriculum

/**
 *
 * @ORM\OneToMany(targetEntity="tbl_enrolled_subjects", mappedBy="id")
 */
private $enrollers;
like image 42
Gabriel Avatar answered Sep 20 '22 12:09

Gabriel