Illustration of the various uses of the SQL JOIN

I. Summary

JOIN for people who come into contact with the database, the word is not strange, but a lot of people very clearly a variety of JOIN, there are a lot of people on this understanding is not very thorough, this time to talk about JOIN operation.

The picture is very easy to be accepted and understood, so try to use pictures to explain.

Two, JOIN classification

Keguan: Small Second, the classification JOIN!

……

Small II: Keguan, freshly baked JOIN classified pictures myself.

Three, JOIN Detailed classification

Keguan: Small Second, come quickly to a detailed road!

Small II: Now let small two to detail to you.

INNER JOIN:

Returns only two tables, the column values ​​match the same column, data of the row.

SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.Col1 = t2.Col1

LEFT OUTER JOIN:

Left outer: returns all the data left table and the right table does not match the column values, which line is sitting nulls.

SELECT * FROM Tables1 t1 LEFT OUTER JOIN Table2 t2 on t1.Col1 = t2.Col2

LEFT OUTER JOIN - WHERE NULL:

Returns all the data rows and the right table does not match

SELECT * FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t2.Col1 IS NULL

RIGHT OUTER JOIN:

Right external connection: the right to return all data tables, and in the left table column values ​​do not match, a line is used which is made null.

SELECT * FROM Tables1 t1 RIGHT OUTER JOIN Table2 t2 on t1.Col1 = t2.Col2

RIGHT OUTER JOIN – WHERE NULL:

Returns all rows that do not match and left the table.

SELECT * FROM Table1 t1 RIGHT OUTER JOIN Table2 t2 ON t1.Col1 = t2.Col1 WHERE t1.Col1 IS NULL

FULL OUTER JOIN:

Fully connected can be regarded as the left and right outer outside Results and connected, two tables returns all the data, if the matching column values ​​match in both tables, then the return data line, otherwise it returns a null value.

SELECT * FROM Table1 t1 FULL OUTER JOIN Table2 t2 ON t1.Col1 = t2.Col1

FULL OUTER JOIN – WHERE NULL:

Data lines other than the return connections, i.e. match all the data other than the column lines sat.

SELECT * FROM Table1 t1 FULL OUTER JOIN Table2 t2 ON t1.ID = t2.ID WHERE t1.ID IS NULL OR t2.ID IS NULL

CROSS JOIN:

Cross-connect does not require any connection conditions. The two data tables will Cartesian product operation.

SELECT * FROM Table1 t1 CROSS JOIN Table2 t2

Guess you like

Origin juejin.im/post/5de616546fb9a016153dc3ca
Recommended