Connection of database tables (left JOIN, right join, Inner join) usage

Source: Internet
Author: User
Tags null null

The LEFT join is a shorthand for the left outer join, and the left join defaults to the outer property.
Inner Join
The Inner join logical operator returns each row that satisfies the first (top) input and the second (bottom) input join. This is the same effect as querying multiple tables with Select, so it is seldom used;
Outer join returns a row for each join that satisfies the first (top) input and the second (bottom) input. It also returns any row in the first input that does not have a matching row in the second input. The key is the latter sentence, return more. So a left join in the usual sense is the left outer join.

First, let's look at some of the simplest examples.

Example

Table A
Aid Adate
1 A1
2 A2
3 A3

TableB

Bid Bdate
1 B1
2 B2
4 B4
Two tables A, B connected, to remove fields with the same ID
SELECT * from a INNER JOIN B on a.aid = B.bid This is only to remove matching data.
At this point, the removal is:
1 A1 B1
2 A2 B2

Then the left join means:
SELECT * from aLeft join B on a.aid = B.bid
First remove all the data from the a table, and then add the data that matches the A/b
At this point, the removal is:
1 A1 B1
2 A2 B2
3 A3 NULL character

The same is trueRight join
Refers to the first to remove all the data in the B table, and then add the data that matches the
At this point, the removal is:
1 A1 B1
2 A2 B2
4 NULL character B4

Left JOIN or left OUTER join.
The result set of the left outer join includes all rows of the left table specified in the OUTER clause, not just the rows that match the joined columns. If a row in the left table does not have a matching row in the right table, all select list columns in the right table in the associated result set row are null values

Two. Left Join/right Join/inner Join operation Demo

Table A records the following:
AID Anum
1 a20050111
2 a20050112
3 a20050113
4 a20050114
5 a20050115

table B records the following:
BID bname
1 2006032401
2 2006032402
3 2006032403
4 2006032404
8 2006032408

The experiment is as follows:
1. Left Join
The SQL statements are as follows:

SELECT * fromA
LeftJOINB
on A.aid=B.bid


The results are as follows:
AID Anum BID bname
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
5 a20050115 NULL NULL
(The number of rows affected is 5 rows)

Result Description:
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.

2. Right Join
The SQL statements are as follows:

SELECT* from A
RightJOINB
onA.aid=B.bid


The results are as follows:
AID Anum BID bname
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL NULL 8 2006032408
(The number of rows affected is 5 rows)

Result Description:
Looking closely, you will find that the result of the left join is exactly the opposite, this time it is based on the right table (B), where a table is not enough to fill with null.

3.inner Join
The SQL statements are as follows:

SELECT * from A
Innerjoin B
on A.aid = B.bid


The results are as follows:
AID Anum BID bname
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404


Connection of database tables (left JOIN, right join, Inner join) usage

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.