inner join, left join, right join

1. Theory

Public field as long as the two tables have matching values, these will be two records in the table are combined.

Personal understanding: to find a common field in both tables meet the requirements of the intersection, and each table records that meet the requirements of a common field for the traction combined.

grammar

FROM table1 INNER JOIN table2 ON table1 . field1 compopr table2 . field2

INNER JOIN operation comprising the following parts:
Part Description
table1, which records the name of the table to be combined table2.
field1, field2 name to join the field. If they are not numbers, the data type of these fields must be identical, and contain the same data, but they need not have the same name.
compopr any relationship comparison operator: "=", "<", ">", "<=", "> =", or "<>."

Explanation

It may operate in any use of the FROM clause INNER JOIN. This is the most common type of join. As long as there exists a matched value of the common field in two tables, Inner coupling will record a combination of these tables.

INNER JOIN can be used Departments and Employees tables to select all the employees in each department. And to select all parts (even if has not been assigned certain sectors of employees) or all employees (even if some employees are not assigned to any department), you can create an outer join by LEFT JOIN or RIGHT JOIN operation.

If you try to join fields containing Memo or OLE Object data, an error will occur.

Two may be coupled to any similar type of numeric fields. For example, numbers can be automatically coupled and long integer field, because they are of a similar type. However, the coupling can not be single-precision and double-precision type field.

The following example shows how the coupling field CategoryID Categories and Products table:

SELECT CategoryName, ProductName

FROM Categories INNER JOIN Products

ON Categories.CategoryID = Products.CategoryID;

In the previous example, it is coupled to the CategoryID field, but it is not included in the query output, because it is not included in the SELECT statement. To include the coupled field, containing the field name in the SELECT statement, in the present embodiment refers Categories.CategoryID.

You can also link several ON clauses in a JOIN statement, use the following syntax:

SELECT fields
FROM table1 INNER JOIN table2
ON table1.field1 compopr table2.field1 AND
ON table1.field2 compopr table2.field2) OR
ON table1.field3 compopr table2.field3)];

You can also nest JOIN statements by 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;

LEFT JOIN or RIGHT JOIN INNER JOIN can be nested in, but not nested in INNER JOIN LEFT JOIN or RIGHT JOIN.

2. Example Operation

Recorded as follows in Table A:
aID in ANUM
. 1 a20050111
2 a20050112
. 3 a20050113
. 4 a20050114
. 5 a20050115

Table B recorded as follows:
bID bname
. 1 2006032401
2 2006032402
. 3 2,006,032,403
. 4 2006032404
. 8 2006032408

Experiments follows:
1.left the Join

sql statement as follows:
SELECT * from A
left the Join B
ON A.aID = B.bID

The results are as follows:
aID in ANUM bID bname
. 1 a20050111 2,006,032,401. 1
2 2 a20050112 2,006,032,402
. 3 a20050113 2,006,032,403. 3
. 4. 4 a20050114 2006032404
5 a20050115 NULL NULL
(the number of affected rows 5 rows)

Results Description:
               left the Join is recorded based on Table A, Table A can be seen as the left, B can be seen at right, left join table is subject to the left.
In other words, the left table (A) of the record It will be fully represented, while the right table (B) will only show records that meet the search criteria (for example: A.aID = B.bID).
insufficient B table where records are NULL.

The Join 2.Right
SQL statement is as follows:
SELECT * from A
right the Join B
ON A.aID = B.bID
results were as follows:
aID in ANUM bID bname
. 1. 1 a20050111 2006032401
2 2 a20050112 2,006,032,402
. 3 a20050113 2006032403. 3
. 4. 4 2,006,032,404 a20050114
NULL NULL. 8 2006032408
(number of rows affected is 5 lines)
results show that:
        a closer look, you will find, and the result is just the opposite left join, right join is the right table (B) based on the lack of local a table filled with NULL .

The Join 3.inner
SQL statement is as follows:
SELECT * from A
INNER JOIN B
ON A.aID = B.bID

The results are as follows:
aID in ANUM bID bname
. 1 a20050111 2,006,032,401. 1
2 2 a20050112 2006032402
. 3 a20050113 2006032403. 3
. 4. 4 2006032404 a20050114

The results Note:
        Obviously, only here the record A.aID = B.bID this description is not to Who basic inner join, it only displays the matching records. 

Reproduced in: https: //www.cnblogs.com/henryhappier/archive/2010/01/25/1656152.html

Guess you like

Origin blog.csdn.net/weixin_33795093/article/details/93537469