2013-12-17 78 views
0

我有相同的行類型的兩個臨時表:比較具有相同的行類型的兩個表的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.

我可以很容易地用循環做到這一點,但我知道循環相當緩慢,我想知道是否有一個簡單的方法來做到這一點,只是與選擇語句。

+0

'...和歸還所有物品...'請定義'items'。 –

回答

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

第二個技巧,謝謝 –

相關問題