码迷,mamicode.com
首页 > 数据库 > 详细

Mysql开发技巧之删除重复数据

时间:2015-11-22 12:38:32      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

Mysql利用联表查询和分组来删除重复数据

//删除表中重复的id,保留最大的id
mysql> select * from user;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  4 | d    |
|  5 | a    |
|  6 | a    |
|  7 | c    |
|  8 | d    |
+----+------+
8 rows in set (0.08 sec)
mysql> delete a from user a left join (select max(id) as id, name from user group by name having(count(id)) > 1)  b on a
.name = b.name where a.id < b.id;
Query OK, 4 rows affected (0.13 sec)

//删除重复数据成功 mysql
> select * from user; +----+------+ | id | name | +----+------+ | 2 | b | | 6 | a | | 7 | c | | 8 | d | +----+------+

 

Mysql开发技巧之删除重复数据

标签:

原文地址:http://www.cnblogs.com/powerCJ/p/4985566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!