An analysis of the difference between SQL left JOIN, right join and INNER join-database other

Source: Internet
Author: User
Tags null null

Chat with a friend today and talk about a small problem in their company. As follows:

Table A equipment table, storage MAC address, province, city, district.
Table B software table, store MAC address, software name.
The function is to query the list of software by province, city, or district.

What do you think it does now?

It obtains the MAC address through the province, the city, or the district, then inquires the B table to use in the query.
It is obviously unreasonable to deal with this many-to-many relationship, so why not use more forms of joint investigation?
The methods commonly used in the list are 3: (inner) join internal equivalent connection, left join and right join.

What difference does it have? How to use it? Here is a copy of an article:

Left JOIN, which returns records that include all records in the left table and the join fields in the right table
Right join returns records that include all the records in the right table and the join fields in the left table
INNER JOIN (equivalent join) returns only rows with the same join field in two tables

Examples are as follows:

Table A records as follows:

Copy Code code as follows:

AID Anum

1 a20050111

2 a20050112

3 a20050113

4 a20050114

5 a20050115


Table B is recorded as follows:
Copy Code code as follows:

BID bname

1 2006032401

2 2006032402

3 2006032403

4 2006032404

8 2006032408

1.left Join

The SQL statement is as follows:

Copy Code code as follows:

SELECT * from A LEFT join B on a.aid = B.bid

The results are as follows:
Copy Code code 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)


Results show:

The left join is based on the records of a table, and a can be viewed as the left-hand table, B can be viewed as the right table, and left table.

In other words, the records in 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).

b tables where the records are insufficient are null.

2.right Join

The SQL statement is as follows:

Copy Code code as follows:

SELECT * from A right join B on a.aid = B.bid

The results are as follows:
Copy Code code 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)


Results show:

With a closer look, you will find that the result of the left join is just the opposite, this time based on the right table (B), where a table is deficient with null padding.

3.inner Join

The SQL statement is as follows:

Copy Code code as follows:

SELECT * from A innerjoin B on a.aid = B.bid

The results are as follows:
Copy Code code as follows:

AID Anum BID bname

1 a20050111 1 2006032401

2 a20050112 2 2006032402

3 a20050113 3 2006032403

4 a20050114 4 2006032404


Results show:

Obviously, only the record of A.aid = B.bid is shown here. This shows that inner join is not based on who, it only shows the records that match the criteria.

Note:

The left JOIN operation is used to combine records from the source table in any from clause. Use the left JOIN operation to create a left-hand outer join. The left outer join will contain all records from the first (left) two tables, even if there are no records in the second (right) table that match the values.

Syntax: SELECT from table1 left JOIN table2 on table1.field1 compopr table2.field2

Description

Table1, the table2 parameter is used to specify the name of the table to combine records with.

Field1, the Field2 parameter specifies the name of the field being joined. These fields must have the same data type and contain the same type of data, but they do not need to have the same name.

The COMPOPR parameter specifies the relational comparison operator: "=", "<", ">", "<=>=" or "<>".

If you want to join a field that contains Memo data type or OLE Object data type data in a inner JOIN operation, an error occurs.

So, as I understand it, SQL should write this:

Select software from software table inner JOIN device table on software table. mac= device table. mac where device table. Pro = ' xxx ' and device table. City = ' xxx ';

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.