postgresql删除重复记录的一些相关sql语句

自己在做postgresql中作的正确的语法,SQL语句

原始表test_sqlpostgresql删除重复记录的一些相关sql语句_第1张图片

1、查询重复字段的重复数select distinct (f1,f2,f3), count(*) from test_sql group by(f1,f2,f3)

结果图片2

2、select distinct (f1,f2,f3), count(*) from test_sql group by(f1,f2,f3) having count(*) >1

结果图片3

3、select distinct f1,f2,f3, count(*) from test_sql group by (f1,f2,f3)having count(*) >1 这样报错,由于有多个字段distinct后加括号才能对

4、select (f1,f2,f3),min(id) from test_sql group by (f1,f2,f3) havingcount(*) >1 前面的(f1,f2,f3)是不能去括号的,也不能单独查询1个,因为group by (f1,f2,f3),相当于把(f1,f2,f3)作为1个字段了。

结果图片4

5、select * from test_sql where id in (select min(id) from test_sqlgroup by (f1,f2,f3) having count(*) >1)

结果图片5

6、select * from test_sql where id in (select min(id) from test_sqlgroup by (f1,f2,f3))

结果图片6

7、delete from test_sql where id not in (select min(id) from test_sqlgroup by (f1,f2,f3))

结果图片7

你可能感兴趣的:(postgresql)