PostgreSQL Joins 子句用于合并数据库中两个或多个表中的记录, JOIN是一种通过使用每个表的公用值来组合两个表中的字段的方法。
PostgreSQL中的联接类型是-
- CROSS JOIN
- INNER JOIN
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
- FULL OUTER JOIN
在继续之前,让无涯教程考虑两个表,COMPANY和DEPARTMENT,无涯教程已经看到了INSERT语句来填充COMPANY表,所以让无涯教程假设COMPANY表中可用的记录列表-
id | name | age | address | salary | join_date ----+-------+-----+-----------+--------+----------- 1 | Paul | 32 | California| 20000 | 2001-07-13 3 | Teddy | 23 | Norway | 20000 | 4 | Mark | 25 | Rich-Mond | 65000 | 2007-12-13 5 | David | 27 | Texas | 85000 | 2007-12-13 2 | Learnfk | 25 | Texas | | 2007-12-13 8 | Paul | 24 | Houston | 20000 | 2005-07-13 9 | James | 44 | Norway | 5000 | 2005-07-13 10 | James | 45 | Texas | 5000 | 2005-07-13
另一个表是DEPARTMENT,具有以下定义-
CREATE TABLE DEPARTMENT( ID INT PRIMARY KEY NOT NULL, DEPT CHAR(50) NOT NULL, EMP_ID INT NOT NULL );
这是用于填充DEPARTMENT表的INSERT语句的列表-
INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID) VALUES (1, 'IT Billing', 1 ); INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID) VALUES (2, 'Engineering', 2 ); INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID) VALUES (3, 'Finance', 7 );
最后,无涯教程在DEPARTMENT表中有以下记录列表可用-
id | dept | emp_id ----+-------------+-------- 1 | IT Billing | 1 2 | Engineering | 2 3 | Finance | 7
CROSS JOIN 联接
CROSS JOIN将第一个表的每一行与第二个表的每一行匹配,如果输入表分别具有x和y列,则输出表将具有x + y列。由于CROSS JOINs可能会生成非常大的表,因此必须注意仅在适当的时候使用它们。
以下是CROSS JOIN的语法-
SELECT ... FROM table1 CROSS JOIN table2 ...
根据上表,无涯教程可以编写一个CROSS JOIN,如下所示:
testdb=# SELECT EMP_ID, NAME, DEPT FROM COMPANY CROSS JOIN DEPARTMENT;
上面给出的查询将产生以下输出-
emp_id| name | dept ------|-------|-------------- 1 | Paul | IT Billing 1 | Teddy | IT Billing 1 | Mark | IT Billing 1 | David | IT Billing 1 | Learnfk | IT Billing 1 | Paul | IT Billing 1 | James | IT Billing 1 | James | IT Billing 2 | Paul | Engineering 2 | Teddy | Engineering 2 | Mark | Engineering 2 | David | Engineering 2 | Learnfk | Engineering 2 | Paul | Engineering 2 | James | Engineering 2 | James | Engineering 7 | Paul | Finance 7 | Teddy | Finance 7 | Mark | Finance 7 | David | Finance 7 | Learnfk | Finance 7 | Paul | Finance 7 | James | Finance 7 | James | Finance
INNER JOIN 联接
INNER JOIN通过基于连接断言组合两个表(table1和table2)的列值来创建新的输出表,该查询将table1的每一行与table2的每一行进行比较,以找到满足联接断言的所有行对,当满足连接断言时,table1和table2的每对匹配的行对的列值将合并为输出行。
INNER JOIN是最常见的联接类型,也是默认的联接类型,您可以选择使用INNER关键字。
以下是INNER JOIN的语法-
SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_filed = table2.common_field;
根据上表,无涯教程可以编写一个INNER JOIN,如下所示:
testdb=# SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;
上面给出的查询将产生以下输出-
emp_id | name | dept --------+-------+------------ 1 | Paul | IT Billing 2 | Learnfk | Engineering
LEFT OUTER JOIN 联接
SQL标准定义了三种类型的外部联接:LEFT,RIGHT和FULL,而PostgreSQL支持所有这些联接。
如果是LEFT OUTER JOIN,则首先执行内部联接,然后,对于不满足与表T2中任何行的联接条件的表T1中的每一行,将在T2的列中添加具有空值的联接行。因此,对于T1中的每一行,联接表始终至少具有一行。
以下是LEFT OUTER JOIN的语法-
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON conditional_expression ...
基于上表,无涯教程可以编写一个内部联接,如下所示:
testdb=# SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;
上面给出的查询将产生以下输出-
emp_id | name | dept --------+-------+------------ 1 | Paul | IT Billing 2 | Learnfk | Engineering | James | | David | | Paul | | Mark | | Teddy | | James |
RIGHT OUTER JOIN 联接
首先,执行内部联接,然后,对于表T2中不满足与表T1中任何行的连接中的每一行,将在T1列中添加具有空值,输出表在T2中的每一行将始终有一行。
以下是RIGHT OUTER JOIN的语法-
SELECT ... FROM table1 RIGHT OUTER JOIN table2 ON conditional_expression ...
基于上表,无涯教程可以编写一个内部联接,如下所示:
testdb=# SELECT EMP_ID, NAME, DEPT FROM COMPANY RIGHT OUTER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;
上面给出的查询将产生以下输出-
emp_id | name | dept --------+-------+-------- 1 | Paul | IT Billing 2 | Learnfk | Engineering 7 | | Finance
FULL OUTER JOIN 联接
首先,执行内部联接,然后,对于不满足与表T2中任何行的联接条件的表T1中的每一行,将在T2的列中添加具有空值,另外,对于不满足与T1中任何行的联接条件的T2的每一行,将在T1列中添加具有空值的联接行。
以下是FULL OUTER JOIN的语法-
SELECT ... FROM table1 FULL OUTER JOIN table2 ON conditional_expression ...
基于上表,无涯教程可以编写一个内部联接,如下所示:
testdb=# SELECT EMP_ID, NAME, DEPT FROM COMPANY FULL OUTER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID;
上面给出的查询将产生以下输出-
emp_id | name | dept --------+-------+--------------- 1 | Paul | IT Billing 2 | Learnfk | Engineering 7 | | Finance | James | | David | | Paul | | Mark | | Teddy | | James |
参考链接
https://www.learnfk.com/postgresql/postgresql-using-joins.html
标签:JOIN,无涯,Joins,DEPARTMENT,PostgreSQL,Paul,联接,OUTER,ID From: https://blog.51cto.com/u_14033984/8957354