postgresql找到表中重复数据的行并删除
生活随笔
收集整理的這篇文章主要介紹了
postgresql找到表中重复数据的行并删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一,創建測試表并插入數據
create table aaa(id bigserial,col1 varchar(255)); insert into aaa values(1,'b'),(2,'a'),(3,'b'),(4,'c'); select * from aaa;二,找到重復行并刪除
方法1:ctid表示數據行在它所處的表內的物理位置,ctid由兩個數字組成,第一個數字表示物理塊號,第二個數字表示在物理塊中的行號。
select * from aaa where ctid not in(select max(ctid) from aaa group by col1);刪除重復行
delete from aaa where ctid not in(select max(ctid) from aaa group by col1);方法2:利用exists
找到重復行
刪除重復行
delete from aaa t1 where exists (select 1 from aaa t2 where t1.col1=t2.col1 and t1.id<t2.id )總結
以上是生活随笔為你收集整理的postgresql找到表中重复数据的行并删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7-3 公路村村通(30 分)
- 下一篇: 小程序之修改引用的vant组件样式(包括