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
);
此博客主要用于记录相关知识点,大部分内容来自网络文章,特此感谢各位作者