[Advanced SQL (c)] [SQL connection (JOIN)]

Previous: Advanced SQL [(b)] [the IN SQL operators operator + SQL the BETWEEN + SQL alias]

SQL connection (JOIN)

It is used to combine SQL join rows from two or more tables.
The following figure shows the seven kinds of usage LEFT JOIN, RIGHT JOIN, INNER JOIN , OUTER JOIN relevant.
Here Insert Picture Description
SQL JOIN clause is used to combine two or more rows from the table, the common field between the tables based.

The most common type of JOIN: SQL INNER JOIN (simple JOIN).
SQL INNER JOIN JOIN returns all rows satisfying the conditions from the plurality of tables.

Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

Different SQL JOIN

Different SQL JOIN type used:
the INNER JOIN: If the table has at least one match, the return line
LEFT JOIN: the right table even if there is no match, it returns all rows from the left table
RIGHT JOIN: even if there is no match left table, from right table returns all rows
FULL JOIN: As long as there is a match where a table, return line

SQL INNER JOIN keywords

INNER JOIN keyword return line is present in at least one matching table.

grammar

SELECT column_name(s)

FROM table1

INNER JOIN table2 ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)

FROM table1

JOIN table2 ON table1.column_name=table2.column_name;

Here Insert Picture Description

SQL LEFT JOIN Keyword

LEFT JOIN keyword from the left table (table1) returns all rows, even if the right table (table2) does not match. If there is no match in the right table, the result to NULL.

grammar

SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)

FROM table1

LEFT OUTER JOIN table2

ON table1.column_name=table2.column_name;

Here Insert Picture Description
Table on the left websites, the right table is access_log. Right table no data will be displayed.
Here Insert Picture Description
LEFT JOIN keyword returns all rows from the left table, right table even if there is no match.

SQL RIGHT JOIN Keyword

RIGHT JOIN right keyword table (table2) returns all rows, even if the left table (table1) does not match. If there is no match left table, the result is NULL.

grammar

SELECT column_name(s)

FROM table1

RIGHT JOIN table2

ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)

FROM table1

RIGHT OUTER JOIN table2

ON table1.column_name=table2.column_name;

Here Insert Picture Description

access_log as the left table, websites as the right table

Here Insert Picture Description
RIGHT JOIN keyword returns all rows from the right table, even if the table does not match left.

SQL FULL OUTER JOIN keywords

FULL OUTER JOIN keyword as long as there is a match left table (table1) and the right table (Table2) wherein a table, the row is returned.

FULL OUTER JOIN keyword combines the results of LEFT JOIN and RIGHT JOIN.

grammar

SELECT column_name(s)

FROM table1

FULL OUTER JOIN table2

ON table1.column_name=table2.column_name;

Here Insert Picture Description

MySQL does not support FULL OUTER JOIN.

FULL OUTER JOIN keyword returns the left and right tables all rows.
If no match "websites" table row "access_log" or "access_log" table row "websites" does not match the table will list these lines.

Published 96 original articles · won praise 164 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42893334/article/details/104675269