postgresql join(连接)——迹忆客-ag捕鱼王app官网
postgresql join 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
在 postgresql 中,join 有五种连接类型:
- cross join :交叉连接
- inner join:内连接
- left outer join:左外连接
- right outer join:右外连接
- full outer join:全外连接
接下来让我们创建两张表 company 和 department。
示例
创建 ,数据内容如下:
jiyik_db# select * from company;
id | name | age | address | salary
---- ------- ----- ----------- --------
1 | paul | 32 | california| 20000
2 | allen | 25 | texas | 15000
3 | teddy | 23 | norway | 20000
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
6 | kim | 22 | south-hall| 45000
7 | james | 24 | houston | 10000
(7 rows)
我们往表里添加几条数据:
insert into company values (8, 'paul', 24, 'houston', 20000.00);
insert into company values (9, 'james', 44, 'norway', 5000.00);
insert into company values (10, 'james', 45, 'texas', 5000.00);
此时,company 表的记录如下:
id | name | age | address | salary
---- ------- ----- -------------- --------
1 | paul | 32 | california | 20000
2 | allen | 25 | texas | 15000
3 | teddy | 23 | norway | 20000
4 | mark | 25 | rich-mond | 65000
5 | david | 27 | texas | 85000
6 | kim | 22 | south-hall | 45000
7 | james | 24 | houston | 10000
8 | paul | 24 | houston | 20000
9 | james | 44 | norway | 5000
10 | james | 45 | texas | 5000
(10 rows)
创建一张 department 表,添加三个字段:
create table department(
id int primary key not null,
dept char(50) not null,
emp_id int not null
);
向 department 表插入三条记录:
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)把第一个表的每一行与第二个表的每一行进行匹配。如果两个输入表分别有 x 和 y 行,则结果表有 x*y 行。
由于交叉连接(cross join)有可能产生非常大的表,使用时必须谨慎,只在适当的时候使用它们。
下面是 cross join 的基础语法:
select ... from table1 cross join table2 ...
基于上面的表,我们可以写一个交叉连接(cross join),如下所示:
jiyik_db=# select emp_id, name, dept from company cross join department;
结果如下:
emp_id | name | dept
-------- ------- --------------------
1 | paul | it billing
1 | allen | it billing
1 | teddy | it billing
1 | mark | it billing
1 | david | it billing
1 | kim | it billing
1 | james | it billing
1 | paul | it billing
1 | james | it billing
1 | james | it billing
2 | paul | engineering
2 | allen | engineering
2 | teddy | engineering
2 | mark | engineering
2 | david | engineering
2 | kim | engineering
2 | james | engineering
2 | paul | engineering
2 | james | engineering
2 | james | engineering
7 | paul | finance
内连接
内连接(inner join)根据连接谓词结合两个表(table1 和 table2)的列值来创建一个新的结果表。查询会把 table1 中的每一行与 table2 中的每一行进行比较,找到所有满足连接谓词的行的匹配对。
当满足连接谓词时,a 和 b 行的每个匹配对的列值会合并成一个结果行。
内连接(inner join)是最常见的连接类型,是默认的连接类型。
inner 关键字是可选的。
下面是内连接(inner join)的语法:
select table1.column1, table2.column2...
from table1
inner join table2
on table1.common_filed = table2.common_field;
基于上面的表,我们可以写一个内连接,如下所示:
jiyik_db=# 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 | allen | engineering
7 | james | finance
(3 rows)
左外连接
外部连接是内部连接的扩展。sql 标准定义了三种类型的外部连接: left、right 和 full, postgresql 支持所有这些。
对于左外连接,首先执行一个内连接。然后,对于表 t1 中不满足表 t2 中连接条件的每一行,其中 t2 的列中有 null 值也会添加一个连接行。因此,连接的表在 t1 中每一行至少有一行。
下面是左外连接( left outer join )的基础语法:
select ... from table1 left outer join table2 on conditional_expression ...
基于上面两张表,我们可以写个左外连接,如下:
jiyik_db=# 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 | allen | engineering
7 | james | finance
| james |
| david |
| paul |
| kim |
| mark |
| teddy |
| james |
(10 rows)
右外连接
首先,执行内部连接。然后,对于表t2中不满足表t1中连接条件的每一行,其中t1列中的值为空也会添加一个连接行。这与左联接相反;对于t2中的每一行,结果表总是有一行。
下面是右外连接( right out join)的基本语法:
select ... from table1 right outer join table2 on conditional_expression ...
基于上面两张表,我们建立一个右外连接:
jiyik_db=# 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 | allen | engineering
7 | james | finance
(3 rows)
外连接
首先,执行内部连接。然后,对于表 t1 中不满足表 t2 中任何行连接条件的每一行,如果 t2 的列中有 null 值也会添加一个到结果中。此外,对于 t2 中不满足与 t1 中的任何行连接条件的每一行,将会添加 t1 列中包含 null 值的到结果中。
下面是外连接的基本语法:
select ... from table1 full outer join table2 on conditional_expression ...
基于上面两张表,可以建立一个外连接:
jiyik_db=# 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 | allen | engineering
7 | james | finance
| james |
| david |
| paul |
| kim |
| mark |
| teddy |
| james |
(10 rows)