自己在做postgresql中作的正确的语法,SQL语句
原始表test_sql
1、查询重复字段的重复数select distinct (f1,f2,f3), count(*) from test_sql group by(f1,f2,f3)
结果
2、select distinct (f1,f2,f3), count(*) from test_sql group by(f1,f2,f3) having count(*) >1
结果
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个字段了。
结果
5、select * from test_sql where id in (select min(id) from test_sqlgroup by (f1,f2,f3) having count(*) >1)
结果
6、select * from test_sql where id in (select min(id) from test_sqlgroup by (f1,f2,f3))
结果
7、delete from test_sql where id not in (select min(id) from test_sqlgroup by (f1,f2,f3))
结果