SQL表内查重和删重

###查询重复记录

select * from my_tab 
 where dup_col in(select dup_col from my_tab
 group 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);
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值