我正在嘗試通過幾個步驟執行一個非常基本的操作:
SELECT資料來自table1- 使用
id我選擇的表中的列來洗掉資料table2 - 將步驟 1 中選擇的表插入
table2
我想這會起作用
begin;
with temp as (
select id
from table1
)
delete from table2
where id in (select id from temp);
insert into table2 (id)
select id from temp;
commit;
但是我收到一條錯誤訊息,說在我的插入步驟中沒有定義 temp?
我發現的唯一其他帖子就是這個,但它并沒有真正回答我的問題。
想法?
uj5u.com熱心網友回復:
來自 Postgres 檔案:
WITH 提供了一種撰寫輔助陳述句以用于更大查詢的方法。這些陳述句,通常被稱為公用表運算式或 CTE,可以被認為是定義了只為一個查詢而存在的臨時表。
如果您需要一個臨時表來處理多個查詢,您可以這樣做:
begin;
create temp table temp_table as (
select id
from table1
);
delete from table2
where id in (select id from temp_table);
insert into table2 (id)
select id from temp_table;
commit;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463055.html
標籤:PostgreSQL psql
