PostgreSQL 表之间连接测试

SQL表操作与联接示例
本文通过创建两个临时表atmp和atmp2,并插入数据,演示了如何使用不同的SQL联接类型来合并这两个表的数据。包括自然联接、内联接、左联接、全联接和右联接等,展示了各种联接方式的特点及应用场景。
create table if not EXISTS atmp(oid int8 PRIMARY key);
INSERT into atmp values(1) on conflict(oid) do update set OID = 1;
INSERT into atmp values(2) on conflict(oid) do update set OID = 2;
INSERT into atmp values(3) on conflict(oid) do update set OID = 3;
INSERT into atmp values(1) on conflict(oid) do update set OID = 1;

create table if not EXISTS atmp2(oid int8 PRIMARY key);
INSERT into atmp2 values(1) on conflict(oid) do update set OID = 1;
INSERT into atmp2 values(4) on conflict(oid) do update set OID = 4;
create table if not EXISTS atmpret(oid int8 PRIMARY key);

select  * from atmp natural join atmp2;
select  * from atmp as a inner join atmp2 as b  on a.oid = b.oid ;
select  * from atmp as a left join atmp2 as b  on a.oid = b.oid ;
select  * from atmp as a full join atmp2 as b  on a.oid = b.oid ;
select  * from atmp as a right join atmp2 as b  on a.oid = b.oid ;
select  * from atmp as a  join atmp2 as b  on a.oid = b.oid ;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值