Mysql中使用SQL删除多字段相同的重复记录

delete from testtable where id in(
    #为了解决You can‘t specify target table ‘test‘ for update in FROM clause错误
	select id from (
        #找出所有符合删除条件的id
		select id from testtable where 
        #条件一
		(name,uid,pay_status) in (
            #数据相同的字段的值
            select name,uid,pay_status from testtable group by name,uid,pay_status having count(*) > 1
        )
		and
        #条件二
		id not in (
            #数据相同的最小id
            select min(id) from testtable group by name,uid,pay_status having count(*) > 1
        )
	) as a
);
SELECT id FROM bop_core_psn_customer_relation GROUP BY customer_id,customer_source_id,customer_source_type,space_id,space_source_id,space_source_type HAVING count(*)> 1;

delete from bop_core_psn_customer_relation where id in(
    #为了解决You can‘t specify target table ‘test‘ for update in FROM clause错误
	select id from (
        #找出所有符合删除条件的id
		select id from bop_core_psn_customer_relation where 
        #条件一
		(customer_id,customer_source_id,customer_source_type,space_id,space_source_id,space_source_type) in (
            #数据相同的字段的值
            select customer_id,customer_source_id,customer_source_type,space_id,space_source_id,space_source_type from bop_core_psn_customer_relation group by customer_id,customer_source_id,customer_source_type,space_id,space_source_id,space_source_type having count(*) > 1
        )
		and
        #条件二
		id not in (
            #数据相同的最小id
            select min(id) from bop_core_psn_customer_relation group by customer_id,customer_source_id,customer_source_type,space_id,space_source_id,space_source_type having count(*) > 1
        )
	) as a
);
posted @   西门长海  阅读(729)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示