2016-01-28 53 views
0

我比java開發人員更多的是java。我已經看着SO這一點,但由於某種原因,WHERE條件SQL插入到ColX表1從表2中選擇ColY其中Table1.colA = Table2.ColB

在PostgreSQL,下面的語法不正確,無法找到類似的問題

INSERT INTO contentelementtypeprogressortype (contentelementtypeclassname) 
SELECT contentelementtypename FROM contentelementtype 
WHERE 
contentelementtype.id=contentelementtypeprogressortype.contentelementtypeid 

的錯誤是:

ERROR: invalid reference to FROM-clause entry for table "contentelementtypeprogressortype" LINE 2: ...OM contentelementtype where contentelementtype.id=contentele... ^ HINT: There is an entry for table "contentelementtypeprogressortype", but it cannot be referenced from this part of the query. ********** Error **********

ERROR: invalid reference to FROM-clause entry for table "contentelementtypeprogressortype" SQL state: 42P01 Hint: There is an entry for table "contentelementtypeprogressortype", but it cannot be referenced from this part of the query. Character: 159

回答

1

這是你的查詢:

INSERT INTO contentelementtypeprogressortype(contentelementtypeclassname) 
    SELECT cet.contentelementtypename 
    FROM contentelementtype cet 
    WHERE cet.id = contentelementtypeprogressortype.contentelementtypeid; 

我不認爲你真的想要一個INSERT - 這增加了一個新的行。我想你真的想要一個UPDATE,它改變了現有的行中的值:

UPDATE contentelementtypeprogressortype cetpt 
    SET contentelementtypeclassname = cet.contentelementtypename 
    FROM contentelementtype cet 
    WHERE cet.id = cetpt.contentelementtypeid; 
+0

愚蠢的我!我會關閉之前,我失去了代表 – Jake

+0

感謝一百萬 – Jake

相關問題