【问题标题】:Compare two tables with same row type PostgreSQL比较具有相同行类型 PostgreSQL 的两个表
【发布时间】:2013-12-18 03:36:48
【问题描述】:

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

items
id - BIGINT
value - float

两个临时表:A 和 B 有:

40 items in Table A
150 items in Table B

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

(a.value - b.value < 5)

进入名为 Table C 的第三个临时表。

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

【问题讨论】:

  • ... and return all items ... 请定义items

标签: sql postgresql loops select


【解决方案1】:

这样的?

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);

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多