簡體   English   中英

如何通過從另一張表中具有相同id的行中的列中獲取值來將值插入到連續行中的表的列中

[英]how to insert values into a column of a table in successive rows by fetching values from a column in a row with a same id in another table

在我的應用程序中,我使用PostgreSQL數據庫。 我有兩個表,即wc_content_definitionsimple_web_content 字段web_content_definition_idwc_content_definition表的主鍵, simple_web_contentsimple_web_content_id的主鍵。 我在simple_web_content添加了名為web_content_definition_id字段,我想用wc_content_definition表中的正確值在所有行中填充此字段。 wc_content_definition表中名為web_content_id的列存儲該行的simple_web_content_id值,而我試圖使用該值來獲取web_content_definition_id並將其值插入simple_web_content表中的相應行中。 我要使用的查詢是:

update simple_web_content set web_content_definition_id = (select 
wcd.web_content_definition_id from  wc_content_definition wcd, 
simple_web_content swc where  simple_web_content_id = wcd.web_content_id ) 
and swc.simple_web_content_id <> 0 and wcd.web_content_id <> 0;

我收到以下錯誤:

錯誤:表“ swc”第3行缺少FROM子句條目:... simple_web_content_id = wcd.web_content_id)和swc.simple ...

錯誤錯誤:缺少表“ swc”的FROM子句條目SQL狀態:42P01字符:209

我正在使用pg-admin工具來執行查詢

update simple_web_content 
set web_content_definition_id =  wcd.web_content_definition_id
from  wc_content_definition wcd 
WHERE simple_web_content_id = wcd.web_content_id 
AND simple_web_content_id <> 0 and wcd.web_content_id <> 0;

在您擁有的查詢中,無法在子查詢之外引用表wc_content_definition

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM