2015-11-06 85 views
0

欲兩個表之間的數據進行比較,如果表的designation值A存在於表乙,我插入的idproduit值中的idproduit對應表B的該指定表A中這樣的:比較兩個表並插入值的PostgreSQL]

table A       |  table B 
--------------------------------- |--------------------------- 
designation|idproduit|date  | | designation |idproduit | 
--------------------------------| |------------------------------ 
des 1  |0  |12/12/2015| |des1   |5 
des 2  |0  |14/06/2015| |de2   |6 
des3  |0  |20/10/2015| | 

我想表B中的idproduit副本值表A時designation是一樣的,我這樣做,但它不工作 PS:我爲我難過英文i 「M法國

Insert into calcul(idproduit) 
(Select idproduit 
     from calcul MT 
     where exists (select designation 
          from produit OT 
          where OT.designation = MT.designation 
         ) 
) 

回答

0

這不是很清楚,但根據您的描述,我想,你真的需要UPDATETableA.idproduct其值從TableB.idproduct其中designation列匹配兩個表。

所以您的查詢可能是以下幾點:

UPDATE table_a a 
SET idproduct = b.idproduct 
FROM table_b b 
WHERE a.designation = b.designation; 

樣品DDL:

CREATE TABLE table_a (
    designation TEXT, 
    idproduct INTEGER, 
    date DATE 
); 

CREATE TABLE table_b (
    designation TEXT, 
    idproduct INTEGER 
); 

INSERT INTO table_a VALUES 
    ('des1',0,'2015-12-12'::DATE), 
    ('des2',0,'2015-06-14'::DATE), 
    ('des3',0,'2015-10-20'::DATE); 

INSERT INTO table_b VALUES 
    ('des1',5), 
    ('des2',6); 
+0

你undrstand我的問題,我有問題,你的要求,B這不是rognizeded – khadi8

+0

我更新的答案示例數據@ khadi8 –

+0

非常感謝您 – khadi8