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 @ 2022-07-18 17:35  西门长海  阅读(716)  评论(0编辑  收藏  举报