多表查询-inner join 、left join、 right join、full join

First, the basic

1.1 SQL queries rationale

First, single-table query: record table according to the filter WHERE condition, an intermediate table (the middle of the table are not visible to the user); and column select the appropriate column returns the final result based on a SELECT selection.
Second, join queries two tables: the table of the two quadrature (Cartesian product) was filtered and the intermediate sheet is formed with an ON condition and connection type; intermediate table record is then filtered according to the WHERE condition, and returns a query result based on a SELECT column specified .
Third, multi-table join queries: first on the first and second table to make queries in the two-table join, then do join queries with query results and a third table, and so on, until all the tables are connected so far , forming an intermediate result table, and then filtered according to the recording intermediate table WHERE condition, and returns a query result based on a SELECT specified column.
SQL query is the process of understanding the theoretical basis for SQL optimization.

The difference between the latter condition 1.2 ON (ON condition) WHERE condition and

ON condition:
is the Cartesian product of the filter is formed in two linked tables constraints middle of the table.
WHERE condition:
in the SELECT statement that the ON condition is filtering constraint middle of the table.
In the absence of a single table query ON, limiting physical table or intermediate result of the query returns records constraints.
Constraint limiting joined to form a final intermediate result table return in two or more tables in the connection table.
As can be seen from here, into the back ON WHERE condition is inappropriate. Recommended practice is: ON only for the connecting operation, WHERE filter records only the intermediate table.

Second, join queries

Operator is used to connect an important way to achieve multi-table queries combined, divided into three types: the connector, the external connector, the cross-connect.

2.1 the connection  INNER JOIN

The connector (INNER JOIN) has two, explicit and implicit, connected back rows in the table and meet the connection conditions of the query. (So-called link table is a database middle of the table to do the query form).

The connection comparison operators (including =,>, <, <>,> =, <=,!> And! <) Comparing operation between the tables, and query data connections that match the conditions. Depending on the comparison method used, the connection is divided into equivalent connections, connections and self-ligation natural three.

Statement 1: Implicit within the connector, without INNER JOIN, the intermediate table is formed to a Cartesian product of two tables.
O.id the SELECT, O.ORDER_NUMBER, c.ID, c.name
the FROM the CUSTOMERS C, the ORDERS O
the WHERE c.ID = O.CUSTOMER_ID;

Statement 2: shows the connector, commonly referred to within the connector, there INNER JOIN, the intermediate table is formed as a Cartesian product of the two tables filtered through ON condition.
O.id the SELECT, O.ORDER_NUMBER, c.ID, c.name
the FROM the INNER the JOIN the ORDERS the CUSTOMERS C = O the ON c.ID O.CUSTOMER_ID;
query statement results statements 3 and 4.

 

Equijoins

"=" Is connecting relationship table query, the query results are listed which are in the connection table for all the columns , which comprises a repeating column .

SELECT
    PM_ACT_JOB_RLS.*, PM_ACT_RLS.*
FROM
    PM_ACT_JOB_RLS
INNER JOIN PM_ACT_RLS ON PM_ACT_JOB_RLS.RlsPK = PM_ACT_RLS.RlsPK

 

Natural connection

Equivalence remove duplicate column connection, the connection is formed.

Really, there is no such connection query value, since it is SQL2 defined in the standard, it gives an example to see it. Natural connection without specifying column connection, the SQL checks whether the same column names two tables, and assuming they use the connection condition, and contains only one column is connected in the connection condition. ON statement is not allowed, not allowed to specify the display column, the column can only display indicated by * (ORACLE environment under test is). For each connection type (except cross-connect), may designate NATURAL.

SELECT
PM_ACT_JOB_RLS.JobPK, PM_ACT_RLS.RlsPK, RlsName 
FROM 
PM_ACT_JOB_RLS 
Natural INNER JOIN PM_ACT_RLS ON PM_ACT_JOB_RLS.RlsPK = PM_ACT_RLS.RlsPK

语句1:
SELECT *
FROM ORDERS O NATURAL INNER JOIN CUSTOMERS C;

语句2:
SELECT *
FROM ORDERS O NATURAL LEFT OUTER JOIN CUSTOMERS C;

语句3:
SELECT *
FROM ORDERS O NATURAL RIGHT OUTER JOIN CUSTOMERS C;

语句4:
SELECT *
FROM ORDERS O NATURAL FULL OUTER JOIN CUSTOMERS C;

 

Since the connection

 If a connection query, two tables are designed to the same table, this query is called self-join queries.

- C1, c2 logically the two tables, a table is physically SELECT
    c1.CategoryID,
    c1.CategoryName
FROM
    [dbo].[Category] c1
INNER JOIN [dbo].[Category] c2 ON c1.[CategoryID] = c2.[ParentID]

 

Joint connection (UNION JOIN)

This is a rare connection. Oracle, MySQL do not support, its role is to: identify the full outer join and connect all the lines within the difference between. This troubleshooting more commonly used in the data analysis. This feature may be realized by using a set of database operations.
Statement 1: Union query (UNION JOIN) sentence, have not found SQL environment that can be executed.
O.id SELECT1, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM the ORDERS O  the UNION the JOIN  the CUSTOMERS C = O.CUSTOMER_ID c.ID the ON

statement 2: 11 achieve equivalent statement under DB2. Do not know whether to support DB2 11 statement it!
O.id the SELECT, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM FULL the ORDERS O c.ID the ON OUTER the JOIN the CUSTOMERS C = O.CUSTOMER_ID
the EXCEPT
the SELECT o.id, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM the ORDERS the JOIN the CUSTOMERS the INNER O = C c.ID the oN O.CUSTOMER_ID;

statement 3: 1 equivalent statement in Oracle's implementation.
O.id the SELECT, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM FULL the ORDERS O c.ID the ON OUTER the JOIN C = O.CUSTOMER_ID the CUSTOMERS
MINUS
SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME
FROM ORDERS O INNER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;

 

2.2 an outer connector

The connecting line is connected only to return data to meet the conditions, and an outer connection line connecting conditions are listed only match, but listed in the left table (left outer connector), the right table (right outer connection) or two tables ( when full outer join all the data matching the search criteria row).

Divided into left outer join outer join, right outer link, full outer join three.

1) LEFT JOIN or LEFT OUTER JOIN 
left outer join result set includes all rows specified LEFT OUTER clause left table, not just the matching join column row. If you do not match the right table rows in a row left the table, select a list of all the rows in the result set associated with the right table columns are null. 
2) RIGHT JOIN or RIGHT OUTER JOIN 
right outer join is the reverse link of the left outer join. Returns all rows in the right table. If there are no matching rows in a row left the table and right table will return a null value for the left table. 
3) FULL JOIN or FULL OUTER JOIN
full outer join to return the left table and all rows in the right table. When a line does not match rows in another table, the other select list column contains a null value . If there is a match between the tables, the entire result set row contains data values of the base table.

语句1:左外连接(LEFT OUTER JOIN)
SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME
FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID;

Statement 2: right outer join (RIGHT OUTER the JOIN)
the SELECT o.id, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM OUTER RIGHT the JOIN the CUSTOMERS the ORDERS O C = the ON c.ID O.CUSTOMER_ID;
Note: ON WHERE condition on the back of the query result is not the same. E.g:

语句3:WHERE条件独立。
SELECT O.ID,O.ORDER_NUMBER,O.CUSTOMER_ID,C.ID,C.NAME
FROM ORDERS O LEFT OUTER JOIN CUSTOMERS C ON C.ID=O.CUSTOMER_ID
WHERE O.ORDER_NUMBER<>'MIKE_ORDER001';

Statement 4: The statement WHERE condition 7 into the back ON.
O.id the SELECT, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM the ORDERS the LEFT OUTER O c.ID the JOIN the CUSTOMERS the ON = C O.CUSTOMER_ID the AND O.ORDER_NUMBER <> 'MIKE_ORDER001';

From the results of the query statements 3 and 4 of the statement, it is clear is not the same, the results showed that 8 statement is incomprehensible. Therefore, it is recommended when writing queries connection, behind only with ON connection conditions, restrictions and conditions on the middle of the table are written to the WHERE clause.

 

Full outer join (FULL OUTER JOIN)

Note: MySQL does not support connections outside the whole, the wording given here for Oracle and DB2. But to get the full outer join query results through the left and right outer external demand collection.
O.id the SELECT, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM FULL the ORDERS O c.ID the ON OUTER the JOIN the CUSTOMERS C = O.CUSTOMER_ID;

collection outside the left and right outer

In practice the above query result and the full outer join statement is the same as
the SELECT o.id, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM the ORDERS the LEFT OUTER O c.ID the JOIN the ON = C O.CUSTOMER_ID the CUSTOMERS
the UNION
the SELECT o.id, O.ORDER_NUMBER, O.CUSTOMER_ID, c.ID, c.name
the FROM OUTER RIGHT the JOIN the CUSTOMERS the ORDERS O C = the ON c.ID O.CUSTOMER_ID;

 

2.3 crossconnect

Cross-connect (CROSS JOIN): There are two explicit and implicit. Without ON clause, return is the product of two tables , called also Cartesian product.

Statement 1: Implicit cross-connect, without CROSS JOIN.
O.id the SELECT, O.ORDER_NUMBER, c.ID, c.name
the FROM the ORDERS O, C the CUSTOMERS
the WHERE o.id =. 1;

Statement 2: The cross-connect explicit use CROSS JOIN.
O.id the SELECT, O.ORDER_NUMBER, c.ID,
c.name
the FROM the ORDERS the CROSS the JOIN the CUSTOMERS O C
the WHERE o.id = 1;
statement results 1 and 2 are the same sentence

 

1.4 Examples

(A)  Example 1

a table b table
id name id job parent_id
1 Zhang 3 1 23 1
2 John Doe 2 34 2
3 Wang Wu 3 34 4

Connected 1) 
select a. *, B. *  From a inner join b on a.id = b.parent_id
result is 
a 23 is 1 1. 3 
2 34 is John Doe 2 2 

2) connected to the left 
select a. *, B. * from a left join b on a.id  = b.parent_id
result is 
a 23 is. 1. 1. 3 
2 2 34 is John Doe 2 
And the king Wu null 

. 3) connected to the right 
select a. *, b. * from a right join b on a.id = b.parent_id 
result is 
a 23 is. 1. 1. 3 
2 2 34 is John Doe 2 
null 34 is. 4. 3 

. 4) is fully connected to 
select a. *, b. *  from a full join b on a.id = b.parent_id
The result is 
a 23 is. 1. 1. 3 
2 2 34 is John Doe 2 
null 34 is. 4. 3 
. 3 Wang Wu null

 

(Ii)  Example 2

Database version: Oracle

Table two TESTA, TESTB, TESTC, each A, B

A. En

The connector, i.e., the most common equivalent connection, for example:

SELECT 
FROM  TESTA,TESTB
WHERE  TESTA.A=TESTB.A

result:

An outer connection B.

Divided into left outer join outer join, right outer joins and full outer joins.

1. Left outer or left outer join left join

Left outer match is not plus primary table data, for example on the basis of equivalent connection:

SELECT  *
FROM  TESTA 
LEFT  OUTER  JOIN  TESTB 
ON  TESTA.A=TESTB.A

result:

Three tables left to do outside link:

SELECT  *
FROM  TESTA 
LEFT  OUTER  JOIN  TESTB 
ON  TESTA.A=TESTB.A
LEFT  OUTER  JOIN  TESTC
ON  TESTA.A=TESTC.A
 

 

2. Right outer join right outer join or right join

Right outer connection to be connected plus the table does not match the data on the basis of equijoins

SELECT  *
FROM  TESTA 
RIGHT  OUTER  JOIN  TESTB 
ON  TESTA.A=TESTB.A

 

3. The full outer join full outer join or full join

Full external connection is based on non-matching data equijoins left and right tables are plus.

SELECT 
FROM  TESTA 
FULL  OUTER  JOIN  TESTB
ON  TESTA.A=TESTB.A

 

A more advanced method Second, stored procedures and functions, etc.

slightly

Third, the summary

(1), an outer connector distinction

By definition only, we can clearly distinguish the.

En: refers to the connection result contains only rows that meet the join condition, two tables are involved in the connection should meet the join condition. That it is, in Table 2 returns the fully qualified recorded contents of fields in each table are from the respective tables.

External connection: connection results not only contain rows that meet the join condition, but also contains its own does not meet the conditions of the line. It includes left outer, right outer coupling and total external connections. That is when, returned two fully qualified table record, their records plus two tables, the result set is only one field record table, another table fields use null null fill.

(2) two tables join query, the query based on what kind of choice

Join query is the core of the SQL query, the query to select the connection type connector according to actual needs. If you choose properly, not only can not improve query performance, but will bring some logic errors or poor performance.
1, two equal check column connected to the associated data tables.
2, Col_L is connected to the right outer Col_R of a subset.
3, Col_R is connected to the left outer Col_L of a subset.
4, Col_R Col_L and have another set of intersection, but not mutually when the whole sub-outer another.
5, differencing operation when a joint inquiry.
When more than one table queries, these different connection types can be written one. For example:
the SELECT T1.C1, T2.CX, T3.CY
the FROM Tl a TAB1
the INNER TAB2 the JOIN T2 the ON (= T1.C1 T2.C2)
the INNER TAB3 the JOIN T3 (= T1.C1 t2.c3) the ON
the LEFT OUTER TAB4 the ON the JOIN (T2.C2 = T3.C3);
the WHERE T1.x> T3.Y;
above this exemplary SQL query is a multi-table joins.

Guess you like

Origin www.cnblogs.com/xysun/p/12146193.html