欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

SQL表内查重和删重

发布时间:2025/5/22 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SQL表内查重和删重 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

###查询重复记录

select * from my_tab where dup_col in(select dup_col from my_tabgroup by dup_col having count(dup_col)>1)order by dup_col;

###查询重复记录(多个条件)

select * from my_tab as t1 where ( select count(*) from my_tab t2 where t2.col_1=t1.col_1 and t2.col_2=t1.col_2 and t2.col_3=t1.col_3 and t2.col_4=t1.col_4 )>1;

###删除重复记录(只留一条)
PG:使用ctid

delete from my_tab where dup_col in (select dup_col from my_tab group by dup_col having count(dup_col) > 1) and ctid not in (select min(ctid) from my_tab group by dup_col having count(dup_col)>1); delete from table_1 where (col_1,col_2,col_3) in (select col_1,col_2,col_3 from table_1 group by col_1,col_2,col_3 having count(*) > 1) and ctid not in (select min(ctid) from table_1 group by col_1,col_2,col_3 having count(*)>1);

ORACLE:使用rowid

delete from my_tab where dup_col in (select dup_col from my_tab group by dup_col having count(dup_col) > 1) and rowid not in (select min(rowid) from my_tab group by dup_col having count(dup_col)>1);

总结

以上是生活随笔为你收集整理的SQL表内查重和删重的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。