Summary of the left join discussion on CSDN

Source: Internet
Author: User

There are two tables, a and B. The first two fields are identical: (id int, name varchar (10 )...)
Id name
---------------------
1
2 B
3 c

Do you know the running result of the following query statement? :
1.
Select * from a left join B on a. id = B. id where a. id = 1
2.
Select * from a left join B on a. id = B. id and a. id = 1
3.
Select * from a left join B on a. id = B. id and B. id = 1
4.
Select * from a left join B on a. id = 1

Result:
Id name
--------------------------------------------
1 10 1 10

(1 row (s) affected)

Id name
--------------------------------------------
1 10 1 10
2 20 null
3 30 null

(3 row (s) affected)

ID name
--------------------------------------------
1 10 1 10
2 20 null
3 30 null

(3 row (s) affected)

ID name
--------------------------------------------
1 10 1 10
1 10 2 20
1 10 3 30
2 20 null
3 30 null

(5 row (s) affected)

Ideas:
Left join is nothing more than a left table. It scans matching records in the right table.

First, there are 1st records in the left table.
1
Scan records in the right table according to condition A. ID = 1.
For each record in the right table, it is clear that the. ID = 1 condition is true, so the result of matching the first record is:

1 A 1
1 A 2 B
1 A 3 C

---------------------------------------------
Then scan 2nd records
2 B
For Condition A. ID = 1, there is no matching record in the edge table, so the right table is null.
Therefore, the result of matching 2nd records is
2 B NULL

----------------------------------------------
The matching result is the same as that of the first record.
3 c NULL

 

 

---------------------------------------
Therefore, the final result is five records.
1 a 1
1 a 2 B
1 a 3 c
2 B null
3 C null

Inner join (a typical join operation that uses comparison operators such as = or <> ). Including equal join and natural join.
The inner join uses the comparison operator to match rows in two tables based on the values of the columns in each table. For example, retrieve all rows with the same student ID in the students and courses tables.

Outer Join. Outer Join can be left Outer Join, right outer join, or complete external join.
When an external join is specified in the from clause, it can be specified by one of the following sets of keywords:

Left join or left Outer Join.
The result set of the left Outer Join includes all rows in the left table specified in the left outer clause, not just the rows matched by the join column. If a row in the left table does not match a row in the right table, all selection list columns in the right table in the row of the associated result set are null.

Right join or right outer join.
The right outer join is the reverse join of the left Outer Join. All rows in the right table are returned. If a row in the right table does not match a row in the left table, a null value is returned for the left table.

Full join or full outer join.
The Complete External Join Operation returns all rows in the left and right tables. If a row does not match a row in another table, the selection list column of the other table contains a null value. If there are matched rows between tables, the entire result set row contains the data value of the base table.

Cross join.
Returns all rows in the left table. Each row in the left table is combined with all rows in the right table. Cross join is also called Cartesian product.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.