sql - 从 PostgreSQL 中删除重复项
问题描述
我正在使用以下内容query
来PostgreSQL 10
查找重复条目:
select
column1, column2, count(*)
from mytable
where column3 in ('yes', 'no')
group by column1, column2 having count(*) > 2;
是否可以PostgreSQL
删除重复项,当然每个条目的第一个除外?
解决方案
假设您的表有一个主键:
delete from mytable t
where t.pk <> (select min(t2.pk)
from mytable t2
where t2.column1 = t.column1 and
t2.column2 = t.column2 and
t2.column3 in ('yes', 'no')
);
推荐阅读
- segue - Swift4 Xcode9通过segue在ViewControllers之间传递变量
- xamarin - Syncfusion SfRating ValueChanged 事件到 Xamarin 表单中的命令
- r - 使用二元组创建共现矩阵
- java - 从 android 设备向 android things 发送命令
- ruby-on-rails - ActiveAdmin 中的自定义页面
- angular - mergeMap 函数在 Angular 5 中出错
- unity3d - unity 3rd 人称控制器,旋转噩梦
- java - 如何在 java 中读取 JSON-LD?
- c++ - 在我自己的堆栈中实现顶部的问题
- pyspark - Dataproc:pyspark UDF 的 functools.partial 没有属性“__module__”错误