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

在postgresql中,有以下类型的连接:

  • 内连接(inner join)
  • 左外连接(left outer join)
  • 右外连接(right outer join)
  • 全连接(full outer join)
  • 跨连接(cross join)

postgresql inner join

postgresql内部连接也被称为连接或简单连接。 这是最常见的连接类型。 此连接返回满足连接条件的多个表中的所有行。

如下图表示 -

语法:

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

postgresql inner join示例

表1: employees有以下数据 -

表2: department有以下数据 -

创建另一个表“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);

现在 department 表的数据如下 -

执行以下查询内连接两个表:

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

执行上面查询语句,得到以下结果 -


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