Postgresql 专题
专题目录
您的位置:database > Postgresql专题 > PostgreSQL全外连接
PostgreSQL全外连接
作者:--    发布时间:2019-11-20

外连接是内联的延伸,外连接有三种类型。它们分别如下 -

  • 左外连接
  • 右外连接
  • 全外连接

全外连接

全外连接从左表和左表中返回所有行。 它将null置于不满足连接条件的位置。

语法:

select table1.columns, table2.columns  
from table1  
full outer join table2  
on table1.common_filed = table2.common_field;

如下图所示(蓝色部分) -

示例

看这个例子,现在看看下面一个表1 - employees,具有以下数据。

表2department

创建另一个表:department并插入以下值。

-- table: public.department

-- drop table public.department;

create table public.department
(
  id integer,
  dept text,
  fac_id integer
)
with (
  oids=false
);
alter table public.department
  owner to postgres;

-- 插入数据
insert into department values(1,'it', 1);
insert into department values(2,'engineering', 2);
insert into department values(3,'hr', 7);
insert into department values(10,'market', 10);

现在,department有以下数据 -

执行以下左连接查询:

select employees.id, employees.name, department.dept  
from employees 
full outer join department  
on employees.id = department.id;

得到以下结果 -


从上面图中可以看到,左表(employees)和右表(department)没有匹配上的项全留为空值。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册