在postgresql中,有以下类型的连接:
postgresql内部连接也被称为连接或简单连接。 这是最常见的连接类型。 此连接返回满足连接条件的多个表中的所有行。
如下图表示 -
语法:
select table1.columns, table2.columns
from table1
inner join table2
on table1.common_filed = table2.common_field;
表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;
执行上面查询语句,得到以下结果 -