您的位置:首页 > 数据库 > MySQL

MYSQL删除一个字段相同记录,保留ID最小记录

2014-04-25 15:37 585 查看
mysql> select * from test1;
+------+------+
| id   | c_id |
+------+------+
|    1 | 0013 |
|    2 | 0014 |
|    3 | 0013 |
|    4 | 0013 |
+------+------+
4 rows in set (0.06 sec)

mysql> delete t from test1 t left join
->  (select c_id,min(id) as min_id from test1 group by c_id) t1
->  on t.id=t1.min_id
-> where t1.min_id is null;
Query OK, 2 rows affected (0.06 sec)

mysql> select * from test1;
+------+------+
| id   | c_id |
+------+------+
|    1 | 0013 |
|    2 | 0014 |
+------+------+
2 rows in set (0.00 sec)

mysql>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息