Postgresql

Postgres SQL:從刪除中插入

  • January 5, 2021

我有一張桌子,id,user_id,timestamp我正試圖INSERT進入桌子,user_id從 aDELETE from table where id=some_numbertimestamp. 所以我想要實現的看起來像:

INSERT INTO table (user_id,timestamp)
VALUES (
   (DELETE FROM table WHERE id=some_number returning user_id),
   current_timestamp+'1 day'
)
RETURNING *;

執行此操作時出現語法錯誤…有人可以在這裡幫助我嗎…?

您可以使用數據修改公用表表達式

with deleted as (
 DELETE FROM some_table
 WHERE id = 42
 returning user_id
)
INSERT INTO other_table(user_id, "timestamp")
select user_id, timestamp '2020-01-04 21:32:34'
from deleted;

引用自:https://dba.stackexchange.com/questions/282550