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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)

有很多问题都需要使用相同表left join关联来处理。

上一篇:加字段、删除字段、调整字段顺序


下一篇:Linux LVM逻辑卷配置过程详解(创建,增加,减少,删除,卸载)