postgresql - 将记录从一个表插入到另一个表,然后删除插入的记录
问题描述
我正在使用此查询将记录从一个表插入到另一个表中:
insert into watched_url_queue_work select * from watched_url_queue
在冲突中什么都不做
目标表上的唯一约束意味着并非全部都被插入。
我现在要做的是删除我刚刚插入的所有记录,但我不确定语法。
我想要类似的东西(查询不起作用只是我的猜测):
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)
方法也有效。)
推荐阅读
- reactjs - history.push() 始终关注与上一页相同的窗口视图
- c++ - 有什么方法可以从父类 C++ 调用子类的函数
- google-cloud-sql - 使用 CMEK 加密的 Cloud SQL 跨区域副本
- json - 如何解决 Struts 2 Json Interceptor invalid json 异常
- python-3.x - 使用 pysftp 连接到服务器失败
- javascript - 如何将行从特定行拆分到下一行?
- javascript - 函数中()里面的值是怎么工作的?
- javascript - 是否可以在自定义组件中使用 @keyup 来定位输入元素?
- google-cloud-platform - 模板文件无法使用 Dataflow 批量删除 API 加载
- c# - 使用 Xamarin 的 PreEmptive Dotfuscator