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 by studentName,classId) b);

方法三:参考:http://www.cnblogs.com/nzbbody/p/4470638.html
1、创建一个临时表,选取需要的数据。
2、清空原表。
3、临时表数据导入到原表。
4、删除临时表。
mysql> select * from student;
+----+------+
| ID | NAME |
+----+------+
| 11 | aa |
| 12 | aa |
| 13 | bb |
| 14 | bb |
| 15 | bb |
| 16 | cc |
+----+------+
6 rows in set

mysql> create temporary table temp as select min(id),name from student group by name;
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0

mysql> truncate table student;
Query OK, 0 rows affected

mysql> insert into student select * from temp;
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from student;
+----+------+
| ID | NAME |
+----+------+
| 11 | aa |
| 13 | bb |
| 16 | cc |
+----+------+
3 rows in set

mysql> drop temporary table temp;
Query OK, 0 rows affected
这个方法,显然存在效率问题。



方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:
mysql> create temporary table temp as select min(id) as MINID from student group by name;
Query OK, 3 rows affected
Records: 3 Duplicates: 0 Warnings: 0

mysql> delete from student where id not in (select minid from temp);
Query OK, 3 rows affected

mysql> select * from student;
+----+------+
| ID | NAME |
+----+------+
| 11 | aa |
| 13 | bb |
| 16 | cc |
+----+------+
3 rows in set



方法3:直接在原表上操作,容易想到的sql语句如下:

mysql> delete from student where id not in (select min(id) from student group by name);
执行报错:1093 - You can‘t specify target table ‘student‘ for update in FROM clause
原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
怎么规避这个问题?
再加一层封装,如下:
mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
Query OK, 3 rows affected

mysql> select * from student;
+----+------+
| ID | NAME |
+----+------+
| 11 | aa |
| 13 | bb |
| 16 | cc |
+----+------+
3 rows in set

时间: 2024-11-08 21:50:10

mysql 删除重复数据保留一条的相关文章

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删除重复只保留一条

本来要删除error_barcode表中barcode值重复的只保留一条 表结构如下(PS:id是自增长,该图是删除成功所以id不连续) sql语句如下: DELETE FROM error_barcode WHERE barCode IN ( SELECT barCode FROM error_barcode GROUP BY barCode HAVING count(barCode) > 1 ) AND id NOT IN ( SELECT min(id) FROM error_barcod

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删除重复数据,保留最新的那一条

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

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 所以其他数据哭的命令在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 删除重复数据实例

如何删除重复数据 业务场景:删除评论表中对同一订单同一商品的重复评论,只保留最早的一条. 查看是否存在对于同一订单同一商品的重复评论. SELECT order_id,product_id,COUNT(*) FROM product_comment GROUP BY order_id,product_id HAVING COUNT(*)>1; 备份product_comment表. CREATE TABLE bak_product_comment_18051801 LIKE product_co

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