Usage and difference of mysql left join, right join, and inner join

Source: Internet
Author: User

In mysql, the left join, right join, and inner join queries are commonly used. But some friends know the differences between them, next I will introduce you to you.

LEFT JOIN

Two tables in the join relationship are displayed. After the data table on the left is joined, all data items are listed, regardless of whether there is a corresponding data table on the right. The related examples are as follows:
In the old-style WHERE join syntax, "* =" in the WHERE clause indicates "left join )」.

Old-style WHERE syntax

The Code is as follows: Copy code

Select c. First_Name, C. Last_Name, O. Order_ID FROM Customer as c, Order as o where c. Customer_ID * = O. Order_ID

This syntax is not supported in MySQL by the MySQL development team. There are two reasons they hold: first, this type of WHERE "left join" is rarely used. If you want to use it, you can use the mark ?? The new "left join" syntax for NSI-92; another reason is that the development of MySQL and related staff development time is limited, such less-used syntax, does not seem to need to waste money and manpower to do.

New JOIN Syntax 1

The Code is as follows: Copy code

Select c. First_Name, C. Last_Name, O. Order_Id FORM Customer as c left join Order as o on c. Customer_ID = O. Customer_ID

All records in the Customer data table are listed here. After JOIN, if the corresponding records cannot be found in the Order, the records will be replaced by NULL.
The left join compatibility syntax for the ANSI-92 also has another way of expressing it by USING. The preceding example can be rewritten:

New JOIN syntax 2

The Code is as follows: Copy code

Select c. First_Name, C. Last_Name, O. Orders_ID FORM Customers as c left join Orders as o using Customer_ID

Use USING Customer_ID to replace C. Customer_ID = O. Customer_ID. For more information about this method, see.

NATURAL LEFT JOIN

It is actually left join, difference? E is that all fields that conform to the "join" relationship are automatically selected. Do not use USING or ON clauses for special purposes? E indicates, so the preceding left join example can be rewritten:

New JOIN syntax

The Code is as follows: Copy code

Select c. First_Name, C. Last_Name, O. Orders_ID FORM Customers as c natural left join Orders AS O

Because both MERs and Orders have mermer_id, after the keyword natural left join is passed, the left join is automatically started. This kind of "JOIN" is not common, so it is also the same as left join, only support mark? Real? NSI-92 syntax.

Is there a right join?

Almost every RDMS has the right join syntax, but MySQL does not support the right join syntax. The reason for not supporting this syntax is simple, this is because right join can be achieved after the data table of left join is called and left join is used. For example:

The Code is as follows: Copy code

Select c. FIIRST_NAME FROM MERs as c left join orders as o on C_CUSTOMER_ID = O. CUSTOMER_ID;

Now, to achieve the right join effect, we only need to reconcile the two data tables:

The Code is as follows: Copy code

Select c. FIIRST_NAME from orders as o left join MERs as c on C_CUSTOMER_ID = O. CUSTOMER_ID;

Therefore, there is no need to make a right join in MySQL. Doing a right join may even reduce the database performance.


The following is an example

The Code is as follows: Copy code

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 SQL statements for creating these two tables are as follows:
Create table
AID int (1) AUTO_INCREMENT primary key,
ANum char (20)
)
Create table B (
BID int (1) not null AUTO_INCREMENT primary key,
BName char (20)
)

Insert into
VALUES (1, 'a20050111'), (2, 'a20050112'), (3, 'a20050113'), (4, 'a20050114'), (5, 'a20050115 ′);

Insert into B
VALUES (1, '000000'), (2, '000000'), (3, '000000'), (4, '000000'), (8, '000000 ′);

The experiment is as follows:
1. left join (left join)

The SQL statement is as follows:
SELECT * FROM
Left join B
ON a. aID = B. bID

The result is 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
(The number of affected rows is 5)

Result description:
Left join is based on the records of table A. A can be seen as the left table, B can be seen as the right table, and left join is based on the left table.
In other words, the records in the left table (A) are all expressed, while the right table (B) only displays records that meet the search criteria (in this example:. aID = B. bID ).
All records in Table B are NULL.

2. right join (right join)

The SQL statement is as follows:

The Code is as follows: Copy code
SELECT * FROM
Right joing B
ON a. aID = B. bID

The result is as follows:
AID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404
NULL 8 2006032408
(The number of affected rows is 5)

Result description:
After careful observation, we will find that the result of left join is exactly the opposite. This time, it is based on the right table (B) and is filled with NULL when table A is insufficient.

3. inner join (equal join or inner join)

The SQL statement is as follows:

The Code is as follows: Copy code
SELECT * FROM
Inner join B
ON a. aID = B. bID

Equivalent to the following SQL statement:

The Code is as follows: Copy code
SELECT *
FROM a, B
WHERE a. aID = B. bID

The result is as follows:
AID aNum bID bName
1 a20050111 1 2006032401
2 a20050112 2 2006032402
3 a20050113 3 2006032403
4 a20050114 4 2006032404

Result description:
Obviously, only the records of A. aID = B. bID are displayed here. This indicates that inner join is not based on WHO, and only records meeting the conditions are displayed.
Left join is used in any FROM clause,

Records of the combined source table. Use the left join operation to create a LEFT Outer JOIN. The outer join on the left contains all records in the two tables starting from the first (left), that is
So that there is no record of consistent values in the second (right) table.

Syntax: FROM table1 left join table2 ON table1.field1 compopr table2.field2
Description: The table1 and table2 parameters are used to specify the names of the tables to be combined.
The field1 and field2 Parameters specify the names of joined fields. These fields must have the same data type and contain the same data type, 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 the Memo data type or OLE Object data type in the inner join operation, an error will occur.

Related Article

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.