数据库管理系统 (DBMS) 允许使用连接从多个表中检索数据。
联接主要是两个或多个关系(或表)的笛卡尔积。
SQL 联接大致分为内联接和外联接。内部联接从满足联接条件的表中选择行。但是使用内连接数据,特别是两个表中不满足条件的行都将丢失。外连接可用于防止表中的数据丢失。
外连接类型:
外连接又分为三种类型:左外连接、右外连接和全外连接。这些解释如下。
- 左外连接:
左外连接返回左侧表中的所有行,右侧表的列是空填充的。左外连接从满足连接条件的两个表中检索所有行以及左表中不匹配的行。句法:
SELECT [column1, column2, ....] FROM table1 LEFT OUTER JOIN table2 ON table1.matching_column = table2.matching_column WHERE [condition];
或者
SELECT [column1, column2, ....] FROM table1 LEFT OUTER JOIN table2 ON table1.matching_column = table2.matching_column WHERE [condition];
图解表示:
- 右外连接:
右外连接返回右侧表中的所有行,左侧表的列是空填充的。右外连接从两个表中检索满足连接条件的所有行以及右表中不匹配的行。句法:
SELECT [column1, column2, ....] FROM table1 RIGHT OUTER JOIN table2 ON table1.matching_column = table2.matching_column WHERE [condition];
或者,
SELECT [column1, column2, ....] FROM table1 RIGHT OUTER JOIN table2 ON table1.matching_column(+) = table2.matching_column WHERE [condition];
图解表示:
- 全外连接:
完全外部联接返回两个表中的所有行。当左表中的行不存在匹配的行时,右表的列将被空填充。类似地,当右表中的行不存在匹配的行时,左表的列将被空填充。全外连接是左外连接和右外连接的并集。句法:
SELECT [column1, column2, ....] FROM table1 FULL OUTER JOIN table2 ON table1.matching_column = table2.matching_column WHERE [condition];
图解表示:
例子:
考虑以下员工表,
EMPID | ENAME | EMPDEPT | SALARY |
---|---|---|---|
101 | Amanda | Development | 50000 |
102 | Diana | HR | 40000 |
103 | Bruce | Designing | 30000 |
104 | Steve | Testing | 35000 |
105 | Roger | Analyst | 10000 |
部门表:
DEPTID | DEPTNAME | LOCATION |
---|---|---|
10 | Development | New York |
11 | Designing | New York |
12 | Testing | Washington |
13 | HelpDesk | Los Angeles |
现在,
1. 左外连接查询 –
Select empid, ename, deptid, deptname
from employee
left outer join department
on employee.empdept = department.deptname;
输出:
EMPID | ENAME | DEPTID | DEPTNAME |
---|---|---|---|
101 | Amanda | 10 | Development |
103 | Bruce | 11 | Designing |
104 | Steve | 12 | Testing |
102 | Diana | null | null |
105 | Roger | null | null |
2. 右外连接查询 –
Select empid, ename, deptid, deptname
from employee right outer join department
on employee.empdept = department.deptname;
EMPID | ENAME | DEPTID | DEPTNAME |
---|---|---|---|
101 | Amanda | 10 | Development |
103 | Bruce | 11 | Designing |
104 | Steve | 12 | Testing |
null | null | 13 | HelpDesk |
3. 全外连接查询 –
Select empid, ename, deptid, deptname
from employee full outer join department
on employee.empdept = department.deptname;
EMPID | ENAME | DEPTID | DEPTNAME |
---|---|---|---|
101 | Amanda | 10 | Development |
103 | Bruce | 11 | Designing |
104 | Steve | 12 | Testing |
102 | Diana | null | null |
105 | Roger | null | null |
null | null | 13 | HelpDesk |
左外连接、右外连接、全外连接的区别:
Left Outer Join | Right Outer Join | Full Outer Join |
---|---|---|
Fetches all the rows from the table on the left | Fetches all the rows from the table on the right | Fetches all the rows from both the tables |
Inner Join + all the unmatched rows from the left table |
Inner Join + all the unmatched rows from the right table |
Inner Join + all the unmatched rows from the left table + all the unmatched rows from the right table |
Unmatched data of the right table is lost | Unmatched data of the left table is lost | No data is lost |
SELECT [column1, column2, ….] FROM table1 LEFT OUTER JOIN table2 ON table1.matching_column = table2.matching_column |
SELECT [column1, column2, ….] FROM table1 RIGHT OUTER JOIN table2 ON table1.matching_column = table2.matching_column |
SELECT [column1, column2, ….] FROM table1 FULL OUTER JOIN table2 ON table1.matching_column = table2.matching_column |