MySQL 删除重复数据实例

如何删除重复数据

业务场景:删除评论表中对同一订单同一商品的重复评论,只保留最早的一条。

  1. 查看是否存在对于同一订单同一商品的重复评论。
SELECT order_id,product_id,COUNT(*) FROM product_comment GROUP BY order_id,product_id HAVING COUNT(*)>1;
  1. 备份product_comment表。
CREATE TABLE bak_product_comment_18051801 LIKE product_comment;

INSERT INTO bak_product_comment_18051801 SELECT * FROM product_comment;
  1. 删除同一订单的重复评论。
DELETE a
FROM product_comment a
JOIN(
    SELECT order_id,product_id,MIN(comment_id) AS comment_id
    FROM product_comment
    GROUP BY order_id,product_id
    HAVING COUNT(*)>=2
) b ON a.order_id=b.order_id AND a.product_id=b.product_id
AND a.comment_id>b.comment_id

原文地址:https://www.cnblogs.com/yizhiamumu/p/9055119.html

时间: 2024-10-23 21:49:26

MySQL 删除重复数据实例的相关文章

mysql 删除重复数据的sql语句

CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title,name,content FROM get_review_url GROUP BY no,title,name,content HAVING COUNT(*) > 1) AND id NOT IN (SELECT MIN(id) FROM get_review_url GROUP BY no,tit

mysql 删除重复数据

如题:mysql 数据库删除重复数据 因为是mysql 所以其他数据哭的命令在mysql 中是不能使用的.不要想当然的使用sql 脚本 delete from table1 where field1 in (select field1 from table1 group by field1 having count(field1) > 1) and rowid not in (select min(rowid) from table1 group by field1 having count(f

MYSQL删除重复数据

delete from co_jobinformation cwhere c.name in (select cc.name from co_jobinformation cc group by  cc.name   having count(cc.name) > 1)and rowid not in (select min(rowid) from co_jobinformation e group by e.name having count(e.name )>1) 之前在oracle数据库

mysql 删除重复数据,并保存最新一条数据

删除重复行 1 DELETE FROM ecm_member_login_session 2 WHERE (number , client_code) IN ( 3 SELECT number, client_code FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1) 4 AND update_time NOT IN ( 5 SELECT MAX(update_time) F

MySQL中删除重复数据的简单方法,mysql删除重复数据

MYSQL里有五百万数据,但大多是重复的,真实的就180万,于是想怎样把这些重复的数据搞出来,在网上找了一圈,好多是用NOT IN这样的代码,这样效率很低,自己琢磨组合了一下,找到一个高效的处理方式,用这个方式,五百万数据,十来分钟就全部去除重复了,请各位参考. 第一步:从500万数据表data_content_152里提取出不重复的字段SFZHM对应的ID字段到TMP3表 create table tmp3 as select min(id) as col1 from data_content

mysql删除重复数据,保留最新的那一条

因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如: 以下表,部门表的部门编号出现了重复. 首先判断是不是重复 1 select count(*) from department d 2 3 select count(*) from ( select distinct dept_code from department ) 看以上查出来的数量是不是

MySQL删除重复数据只保留一条

面试碰到一个MySQl的有趣的题目,如何从student表中删除重复名字的行,并保留最小id的记录? 很遗憾当时没有做出来,回家搜索了一番,发现利用子查询的可以很快解决. 1.删除表中多余的重复记录,重复记录是username判断,只留有id最小的记录 delete from studentwhere username in ( select username from studentgroup by username having count(username)>1) and id not i

mysql 删除重复数据保留只保留一条

SELECT * FROM (SELECT addTime FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > 1) AS b) AND ID NOT IN (SELECT * FROM (SELECT MIN(ID) FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > 1) AS c) MIN(ID)表示保留ID最小的一条

mysql 删除重复数据保留一条

验证:mysql 5.6版本 方法一: delete a from table a left join( select (id) from table group by studentName,classId) b on a.id=b.id where b.id is null; 方法二: explain delete from table where id not in (select minid from (select min(id) as minid from table group b