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
);
此博客主要用于记录相关知识点,大部分内容来自网络文章,特此感谢各位作者
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix