Left JOIN multi-table operation

Source: Internet
Author: User

Borrowed a bo friend: http://blog.csdn.net/tomholmes7/article/details/5786166

The table structure is established as follows:

Create Table A (A1 int, a2 varchar (10));
CREATE TABLE B (b1 int, B2 varchar (10));
CREATE TABLE C (c1 int, C2 varchar (10));

Insert into a values (1, ' haha ');
Insert into a values (2, ' ssss ');
Insert into a values (4, ' tttt ');
Insert into B values (1, ' CCCC ');
INSERT into C values (2, ' xxxx ');


Perform the following two left joins:

SELECT * from a
Left JOIN B on A.A1=B.B1
Left join C on A.A1=C.C1;

SELECT * from a
Left JOIN B on A.A1=B.B1
Left join C on B.B1=C.C1;

The result is actually different:

The former is:



The latter is:

Personal Understanding:

The left join is based on the records of Table A, a can be regarded as the right table, and B can be regarded as left table. In other words, the records of the left table (A) will all be represented, and the right table (B) will only display records that match the search criteria (in the example: A.aid = b.bid). The low-record of table B is null.

When the left join is C, it is the temporary table generated with a to join B as the main table and then go to the "join" C table, which I understand.

A LEFT join B on A.A1=B.B1 left join C on a.a1=c.c1 = tied up, basically no problem

Left JOIN multi-table operation

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.