How does Left Join / IS NULL eliminate records whi

2020-03-04 07:05发布

I am having a tough time to understand why does LEFT JOIN / IS NULL eliminate records which are there in one table and not in the other. Here is an example

SELECT  l.id, l.value
FROM    t_left l
LEFT JOIN t_right r
ON      r.value = l.value
WHERE   r.value IS NULL

Why should r.value = NULL eliminate records ? I am not understanding . I know I am missing something very basic but at present I cant figure out even that basic one. I would appreciate if someone explains it to me in detail .

I want a very basic explanation.

11条回答
爷、活的狠高调
2楼-- · 2020-03-04 07:28

Let's assume r-table is employees, and r_table is computers. Some employees don't have computers. Some computers are not assigned to anyone yet.

  1. Inner join:

    SELECT  l.*, r.*
    FROM    employees l
    JOIN computers r
    ON      r.id = l.comp_id
    

    gives you the list of all employees who HAVE a computer, and the info about computer assigned for each of them. The employees without a computer will NOT appear on this list.

  2. Left join:

    SELECT  l.*, r.*
    FROM    employees l
    LEFT JOIN computers r
    ON      r.id = l.comp_id
    

    gives you the list of ALL employees. The employees with computer will show the computer info. The employees without computers will appear with NULLs instead of computer info.

  3. Finally

    SELECT  l.*, r.*
    FROM    employees l
    LEFT JOIN computers r
    ON      r.id = l.comp_id
    WHERE   r.id IS NULL
    

    Left join with the WHERE clause will start with the same list as the left join (2), but then it will keep only those employees that do not have corresponding information in the computer table, that is, employees without the computers.

    I this case, selecting anything from the r table will be just nulls, so you can leave those fields out and select only stuff from the l table:

    SELECT  l.*
    FROM ...
    

Try this sequence of selects and observe the output. Each next step builds on the previous one.

Please let me know if this explanation is understandable, or you'd like me to elaborate some more.

EDITED TO ADD: Here's sample code to create the two tables used above:

CREATE TABLE employees
(  id INT NOT NULL PRIMARY KEY,
   name VARCHAR(20),
   comp_id INT);

INSERT INTO employees (id, name, comp_id) VALUES (1, 'Becky', 1);
INSERT INTO employees (id, name, comp_id) VALUES (2, 'Anne', 7);
INSERT INTO employees (id, name, comp_id) VALUES (3, 'John', 3);
INSERT INTO employees (id, name) VALUES (4, 'Bob');

CREATE TABLE computers
(  id INT NOT NULL PRIMARY KEY,
   os VARCHAR(20) );

INSERT INTO computers (id, os) VALUES (1,'Windows 7');
INSERT INTO computers (id, os) VALUES (2,'Windows XP');
INSERT INTO computers (id, os) VALUES (3,'Unix');
INSERT INTO computers (id, os) VALUES (4,'Windows 7');

There are 4 employees. Becky and John have computers. Anne and Bob do not have a computer. (Anne, has a comp_id 7, which doesn't correspond to any row in computers table - so, she doesn't really have a computer.)

查看更多
闹够了就滚
3楼-- · 2020-03-04 07:28

It's Simple Left join funda..... following Query get all the record from left table and matched record From Right table if it doesn't match than it will return NUll value. at The end you filter data by using where condition which return values that are in Left table but not in right table

 SELECT  l.id, l.value
    FROM    t_left l
    LEFT JOIN t_right r
    ON      r.value = l.value
    WHERE   r.value IS NULL
查看更多
地球回转人心会变
4楼-- · 2020-03-04 07:29

the left returns all the row from the left table

ON r.value = l.value
if there is no r.value for a l.value then the r is empty
r.value is null will be true

查看更多
聊天终结者
5楼-- · 2020-03-04 07:31

This is based on the difference between INNER JOIN and LEFT JOIN. When you use an INNER JOIN, the result only contains rows that match between the two tables (based on the ON condition).

But when you use a LEFT JOIN, you also get rows in the result for all rows in the first table that don't have a match in the second table. In these cases, all the result columns from the second table are filled with NULL as a placeholder.

The WHERE r.value IS NULL test then matches these rows. So the final result only contains the rows with no match.

查看更多
Lonely孤独者°
6楼-- · 2020-03-04 07:34

This sentence:

SELECT  l.id, l.value
FROM    t_left l
LEFT JOIN t_right r
ON      r.value = l.value

will have all the elements from t_left table joined to all elements from t_right table. As you probably will now, in an INNER JOIN only rows which matches both tables will be shown, so t_left could have less rows than all the possible rows. In a LEFT JOIN, you have ALL THE ROWS from t_left table plus the matched rows from t_right (if they can be joined). So, what happens with rows in t_left that not matches any t_right row? All the fields from t_right table that would be supposed to contain data will be filled with NULL values. So, if you only select NULL values from right table, you'll find all the values from the t_left table that has no match on the right one, XD

查看更多
登录 后发表回答