繁体   English   中英

比较具有相同行类型PostgreSQL的两个表

[英]Compare two tables with same row type PostgreSQL

我有两个相同行类型的临时表:

items
id - BIGINT
value - float

两个临时表(称为A和B)具有:

40 items in Table A
150 items in Table B

我想将表A中的每个项目与表B中的每个项目进行比较,并返回其中的所有项目:

(a.value - b.value < 5)

到称为表C的第三个临时表中。

我可以使用循环轻松地做到这一点,但是我知道循环相当慢,我想知道是否有一种简单的方法可以仅使用select语句来完成。

像这样吗

insert into c
select * from a
where exists (select 1 from b where a.value - b.value < 5);

还是您还想要表B中的所有值?

在这种情况下,

insert into c
select * from a
where exists (select 1 from b where a.value - b.value < 5)
union
select * from b
where exists (select 1 from a where a.value - b.value < 5);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM