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
mysql 删除重复数据保留一条的更多相关文章
- SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法
create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...
- Mysql删除重复数据保留最小的id
在网上查找删除重复数据保留id最小的数据,方法如下: DELETE FROM people WHERE peopleName IN ( SELECT peopleName FROM people GR ...
- mysql 删除重复数据保留只保留一条
SELECT * FROM (SELECT addTime FROM motorcade.car_msg_info GROUP BY addTime HAVING COUNT(addTime) > ...
- sql 删除重复数据保留一条
--创建测试表 CREATE TABLE TEST ( DEPTNO ), DNAME ), LOC ) ); --插入测试数据 , 'test1', 'test2'); , 'test1', 'te ...
- sql server删除重复数据,保留第一条
SELECT * FROM EnterpriseDataTools.Enterprise.CompanyMainwhere CompanyNo in (select CompanyNo from En ...
- mysql删除重复数据只保留一条
mysql删除重复数据只保留一条 新建一张测试表: CREATE TABLE `book` ( `id` char(32) NOT NULL DEFAULT '', `name` varchar(10 ...
- mysql 删除重复数据只保留一条记录
删除重复数据保留name中id最小的记录 delete from order_info where id not in (select id from (select min(id) as id fr ...
- mysql删除重复数据,保留最新的那一条
因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如 ...
- mysql 删除重复数据,并保存最新一条数据
删除重复行 DELETE FROM ecm_member_login_session WHERE (number , client_code) IN ( ) AND update_time NOT I ...
随机推荐
- windows nslookup、tracert 常用命令
nslookup www.baidu.com 可以指定查询的类型,可以查到DNS记录的生存时间还可以指定使用哪个DNS服务器进行解释. tracert www.baidu.com 路由
- Python 集合操作
1.集合操作 集合是一个无序的,不重复的数据组合, 他的主要作业如下. 1.去重,把一个列表变成集合,就自动去重了 2.关系测试,测试两组数据之前的交集.差集.并集等关系 list_1 = [1,4, ...
- Java接口回调
public class A { private D d; private C c; public A (C c) { this.c = c; } public void setD (D d) { t ...
- C# httprequest post 内容有百分号,部分特殊字符乱码问题
哎没办法,还没完全脱离.net,已经一半了. http://stackoverflow.com/questions/7908581/how-to-encode-http-post-parameters ...
- 夺命雷公狗-----React---6--props多属性的传递
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- TypeError: unsupported operand type(s) for |: 'str' and 'str'
问题描述:
- 初识python第二天(3)
我们接着上一篇博客,继续来来了解Python一些常见类的函数使用方法 一.int # 运算符,>=,比较self是否大于等于value,只要满足大于或者等于其中一个条件,就返回True,否则就返 ...
- JAVA设计模式——单例模式
单例模式的定义: Ensure a class has only one instance, and provide a global point of access to it.( 确保某一个类只有 ...
- IntelliJ IDEA使用(3)——IDEA连接Git
摘要: Intellij IDEA作为最强大智能的IDE,内部已经集成了Git的功能,所以不用安装插件,连接Git@OSC也非常容易 首先安装git for windows 推荐使用这个:http:/ ...
- C++string中有关大小和容量的函数浅析
1.length()与size() length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符 ...