将记录从一个 table 插入到另一个,然后删除插入的记录

Insert records from one table to another and then delete inserted records

我正在使用此查询将记录从一个 table 插入另一个 table:

insert into watched_url_queue_work select * from watched_url_queue

冲突时什么都不做

目标的唯一约束table意味着并非所有都被插入。

我现在想做的是删除我刚刚插入的所有记录,但我不确定语法。

我想要类似的东西(查询不起作用只是我的猜测):

delete from watched_url_queue q
where q.target_domain_record_id in
      (
        insert into watched_url_queue_work select * from watched_url_queue
        on conflict do nothing
        returning watched_url_queue_work.target_domain_record_id
      )

您可以使用 CTE:

with inserted as (
  insert into watched_url_queue_work
  select * from watched_url_queue
  on conflict do nothing
  returning watched_url_queue_work.target_domain_record_id
)
delete from watched_url_queue q
using inserted
where q.target_domain_record_id = inserted.target_domain_record_id;

q.target_domain_record_id in (select … from inserted) 方法 works as well。)