【小笔记】临时表:删除无主键表中的重复记录,只保留一条记录

1 无主键表-使用临时表

整体思路是先找出重复的记录放入临时表中,然后删除原表中的重复数据,再将临时表中的数据插入进去

找出重复的放入临时表:

create table tmp_table as (select name,age,sex, count(1) as cn from ori_table group by name, age,sex having cn > 1);

删除原表重复的:

delete ori_table from ori_table inner join tmp_table using  (name,age,sex);

插入重复数据:

insert into ori_table  select name,age,sex from tmp_table;

删除临时表:

drop table tmp_table;

2 有主键表-使用id剔除


select count(1) from


# 这个是实际的删除sql,大概思路就是按照id删除,但是剔除最小的那个id
delete from 
test_table where id in 
(
	SELECT id from 
	(
		select id from test_table as t1,
		(
		SELECT p_id,s_id from test_table group by p_id,s_id having COUNT(*) > 1
		) as t2
		where t1.p_id = t2.p_id and t1.s_id = t2.s_id
		# 如果存在为空的情况,还要用isNull
		#where t1.p_id is NULL and t1.s_id = t2.s_id 
	) as  t3
)
and id not in 
(
	SELECT id from 
	(
		SELECT min(id) as id FROM test_table GROUP BY p_id,s_id HAVING count(1) > 1
	) as t4
)


# 计算要删除的数据条目数
SELECT 
(
SELECT SUM(cn) as ct from (
SELECT COUNT(*) as cn , p_id,s_id from test_table group by p_id,s_id having cn > 1
) as t2
) - 
(
SELECT COUNT(id) from 
(
	SELECT min(id) as id FROM test_table GROUP BY p_id,s_id HAVING count(1) > 1
) as t4
) as diffNum

3 使用联表删除:全删除,不保留数据

DELETE test_table from test_table,
(
SELECT COUNT(*) as cn , o_id,s_id from test_table group by o_id,s_id having cn > 1
) as t2 
WHERE test_table.o_id=t2.o_id and test_table.s_id=t2.s_id;
# null也要做特殊处理
WHERE test_table.o_id=t2.o_id and test_table.s_id is null;

PS:
【JAVA核心知识】系列导航 [持续更新中…]
欢迎关注…

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yue_hu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值