Postgresql 删除重复数据

251 阅读1分钟

记一篇短篇文章,记录一下今天遇到的一个问题以及解决办法

解决办法是我在v2ex上问的,感谢V2ex上的朋友。

情况是这样的,我现在手上有一张大表,600 万的数据吧。但是里面一半是重复的。

column 是这样的

id,word ,translation

但是每一项的数据都是重复的,比如

select * from testtable ; 

==>
  
  id ,word,translation 
  1, name1, 数据 1
  1, name1, 数据 22222

因为原先的 id 没有设置唯一约束,导致出现的这么多重复项, id ,word 内容都是一样的,但是 translation 就不一样了。

解决办法是这样的:


delete from testtable 
              where translation in ( 
                    select translation from ( 
                           select t1.translation from testtable t1 
                               join testtable t2  
                               on t2.id=t1.id 
                               where length(t2.translation) > length(t1.translation)
                    ) trans 
              );

=====================================

捕获5.PNG

个人主页: YEE领域

不一样的背单词 APP: 领域英语APP