加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL删除重复项

发布时间:2020-12-13 16:01:55 所属栏目:百科 来源:网络整理
导读:我正在研究postgres查询以从表中删除重复项.下表是动态生成的,我想编写一个select查询,如果第一行有重复值,它将删除记录. 该表看起来像这样 Ist col 2nd col 4 62 6 34 5 26 5 12 我想写一个select查询,删除第3行或第4行. 解决方法 不需要中间表: delete fr
我正在研究postgres查询以从表中删除重复项.下表是动态生成的,我想编写一个select查询,如果第一行有重复值,它将删除记录.

该表看起来像这样

Ist col  2nd col
 4        62
 6        34
 5        26
 5        12

我想写一个select查询,删除第3行或第4行.

解决方法

不需要中间表:

delete from df1
where ctid not in (select min(ctid)
                   from df1
                   group by first_column
                   having count(*) > 1);

如果要从大表中删除许多行,则使用中间表的方法可能更快.

如果您只想获取一列的唯一值,可以使用:

select distinct on (first_column) *
from the_table
order by the_table;

或者干脆

select first_column,min(second_column)
from the_table
group by first_column;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读