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

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

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

右外连接

右外连接返回从“on”条件中指定的右侧表中的所有行,只返回满足条件的另一个表中的行。
如下图中所表示:

语法:

select table1.columns, table2.columns  
from table1  
right 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 
right outer join department  
on employees.id = department.id;

得到以下结果 -

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


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