SQL syntax: Inner join on, left join in, right join on detailed usage method

Source: Internet
Author: User

INNER JOIN (equivalent join) returns only rows that have the same join field in two tables

Left join returns records that include all the records in the left table and the equivalent of the junction fields in the right table

Right join returns records that include all records in the right table and the junction fields in the left table

INNER JOIN Syntax:

INNER join joins two data table usage:

SELECT * FROM table 1 INNER JOIN table 2 on table 1. Field number = Table 2. Field number

INNER join joins three data table usage:

SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER JOIN table 3 on table 1. Field number = Table 3. Field number

INNER join joins four data table usage:

SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER Join

Table 4 on Member. Field number = Table 4. Field number

INNER join joins five data table usage:

SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER join table 4 on Member. Field number = Table 4. Field number) in NER JOIN table 5 on Member. Field number = Table 5. Field number

Connection six data Tables usage: slightly, similar to the above join method, everybody extrapolate bar:)

Precautions:

In the process of entering the alphabet, be sure to use the English half-width punctuation marks, the word between the half-corner space; when you create a table, if one is joined to more than one table, the fields in that table must be "number" data types, and the same fields in multiple tables must be primary keys and "AutoNumber" data types. Otherwise, it is difficult to join successfully. code Nesting Quick Method: For example, if you want to connect five tables, just add a front and back bracket to the code that joins the four tables (the parentheses are appended to the end of the code), and then continue adding "INNER JOIN table name x on table 1 after the parentheses." Field number = table X. Field number "Code, so you can join the data table indefinitely:)

1. Theory

As long as the public fields of the two tables have matching values, the records in the two tables are grouped together.

Personal understanding: Find the intersection of two tables in a common field, and combine each table with the required records in a common field for traction.

Grammar

SELECT * FROM table1 INNER joins table2 on table1. Field1 compopr table2. Field2

The INNER JOIN operation contains the following sections:

Part Description
Table1, table2 The name of the table to which the records are to be combined.
Field1,field2 The name of the field to join. If they are not numbers, these fields must have the same data type and contain homogeneous data, but they do not have to have the same name.
Compopr Any relational comparison operator: "=", "<", ">", "<=", ">=", or "<>".

Description

You can use the INNER JOIN operation in any FROM clause. This is the most common type of join. The Inner join combines the records in these tables as long as there is a matching value on the public field of the two tables.

You can use INNER JOIN for departments and Employees tables to select all employees in each department. Instead, you can create an outer join by using a LEFT JOIN or right JOIN operation to select all the parts (even if there are no employees assigned in some departments) or all employees (even if some employees are not assigned to any department).

If you attempt to join a field that contains Memo or OLE object data, an error occurs.

You can join any two similar types of numeric fields. For example, you can join both AutoNumber and long fields, because they are similar types. However, you cannot join single-precision and double-type fields.

The following example shows how to join Categories and the Products table through the CategoryID field:

SELECT CategoryName, ProductName

From Categories INNER joins products

on Categories.CategoryID = Products.CategoryID;

In the previous example, CategoryID is a joined field, but it is not included in the query output because it is not included in the SELECT statement. To include the joined field, include the field name in the SELECT statement, which in this case refers to Categories.CategoryID.

You can also link multiple on clauses in a JOIN statement, using the following syntax:

SELECT table1 INNER JOIN table2 on table1.field1 compopr table2.field1 and on table1.field2 compopr table2.fie Ld2 OR on table1.field3 compopr table2.field3;

You can also nest JOIN statements with the following syntax:

SELECT fields from table1 INNER join (table2 INNER join [(]table3 [INNER join] [(]tablex [INNER join ...)] On table3.field3 compopr tablex.fieldx)] On table2.field2 compopr table3.field3) on table1.field1 compopr table2.field2;

A LEFT JOIN or right join can be nested within a INNER join, but the INNER join cannot be nested inside a left JOIN or a right join.

2. Operating example

The

Table A is recorded as follows: aid               ANum 1                   a20050111 2                   a20050112 3                   a20050113 4                   a20050114 5                   a20050115

Table B is recorded as follows: BID bname 1 2006032401 2 2006032402 3 2006032403 4 2006032404 8 2006032408

The experiment is as follows: 1.left join

The SQL statement is as follows: select * from A LEFT join 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 5                    a20050115                 null               NULL (the number of rows affected is 5 rows)

The results show that the left join is based on the records of a table, 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 JoinThe SQL statements are as follows:SELECT * from A right join B  on a.aid = B.bid 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.

The 3.inner join SQL statement is 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

Result Description :         Obviously, this shows only the records of A.aid = B.bid. This shows inner Join is not based on who, it only displays records that match the criteria .   and the inner join can be used in conjunction with the WHERE statement   such as:    select * from A innerjoin B on a.aid = b.bid where b.bname= ' 2006032401 ' This will only put back a single piece of data

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.