找回密码
 立即注册
查看: 2266|回复: 0
打印 上一主题 下一主题

mysql删除重复记录,保存Id最小的一条

[复制链接]
跳转到指定楼层
楼主
发表于 2016-8-23 20:58:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法1:; e& c% D0 N; u1 y& O  W$ z
1、创建一个临时表,选取需要的数据。
% Y+ I. S0 W6 M$ L9 t' q2、清空原表。  K6 K# l  @3 p( `' U/ J7 r' j6 {9 }
3、临时表数据导入到原表。
: C) {7 x: \# h3 b2 A4、删除临时表。5 E- M$ A* G4 z- l8 ^
mysql> select * from student;0 Q5 j, G- A- `9 W
+----+------+
9 E* ~: y1 _- r8 K& T, ]2 @| ID | NAME |3 d& u: o6 S& R' W
+----+------+
( ^, `1 ]/ ?# U& ~& d| 11 | aa |
" w0 l+ M, R4 L- k) J| 12 | aa |
1 q( y4 d& c' k& o- ~" l, P5 F| 13 | bb |
# f! B' e; m# r& l* D' {| 14 | bb |
' a) O4 V9 h9 M2 ?| 15 | bb |
$ N6 C8 C, F+ T- P: o/ L: }| 16 | cc |
' a! x7 K# C& |) L5 H+----+------+" c) L- D* r6 Z: v% I( v2 O1 J
6 rows in set
mysql> create temporary table temp as select min(id),name from student group by name;
. N! R4 }" D" J- X: y. s) kQuery OK, 3 rows affected
1 G# o, z- s% i0 }9 GRecords: 3 Duplicates: 0 Warnings: 0
mysql> truncate table student;
8 B6 p& l4 q) U, I2 \4 Z* q: I0 }Query OK, 0 rows affected
mysql> insert into student select * from temp;0 _* r6 [5 X+ L" G/ L+ {+ f1 }
Query OK, 3 rows affected1 h/ y+ F+ @8 f1 A7 I0 m
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;( ^6 L( o$ B# W
+----+------+% p$ k$ o. C8 O( j4 F1 h
| ID | NAME |
% b6 F2 h; @: w+ `/ F+----+------+4 d- V% T) }* M: K+ r2 v
| 11 | aa |
% ]) T6 X/ {0 D& A| 13 | bb |! K7 C) M; A8 F3 B3 X4 s3 \
| 16 | cc |0 ?: S# R4 y- g& w1 o, E
+----+------+2 T: v0 I  T! U% ?1 ]3 `! h
3 rows in set
mysql> drop temporary table temp;
# g" u3 t3 V2 @Query OK, 0 rows affected6 u9 [3 P* V5 t* N2 |9 {7 X
这个方法,显然存在效率问题。

方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:/ I! Y+ U: D" i
mysql> create temporary table temp as select min(id) as MINID from student group by name;
% x  e* _) h- ~$ @& u( XQuery OK, 3 rows affected4 D' n' Z  A1 D# ~# v' t, Q
Records: 3 Duplicates: 0 Warnings: 0
mysql> delete from student where id not in (select minid from temp);
& Y$ }4 o! M3 \6 k; J" G9 pQuery OK, 3 rows affected
mysql> select * from student;
% G" _$ M+ h- }. J( x  a% D+----+------+9 z  e6 R7 v* [1 {( S; t4 @
| ID | NAME |
, j1 k4 A0 |' c+----+------+
. E$ U4 k9 M$ T% ]6 e! n3 ?" f" x% I| 11 | aa |
8 s7 x6 T9 q9 D' D: d| 13 | bb |
, y. ~9 t1 V: a| 16 | cc |+ ], ^+ u# ~! }9 n
+----+------+
! P5 K& T+ z) |( m1 g7 C+ O3 rows in set

方法3:直接在原表上操作,容易想到的sql语句如下:
mysql> delete from student where id not in (select min(id) from student group by name);4 I% q! \  _7 @: V* l4 A, f2 a
执行报错:1093 - You can't specify target table 'student' for update in FROM clause
/ R8 I/ `4 F& X5 i1 q) k原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
& E! |9 j6 q0 _! C怎么规避这个问题?. j1 g2 e0 W! a, Z  l4 J0 D
再加一层封装,如下:
  ^" H% X& c3 W6 Imysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
/ n) H# f6 T7 F" @, BQuery OK, 3 rows affected
mysql> select * from student;5 g1 \% n$ b$ Y8 z
+----+------+# h7 O$ i2 O' |' \# d
| ID | NAME |
7 [% o3 j: y$ I! b# P4 S: R+----+------+1 T$ [7 X& B# G' W
| 11 | aa |: m* C0 B, W9 k
| 13 | bb |
& `1 G+ m* C0 W- }  e, n| 16 | cc |
. z" ]: Z9 t0 X  ]; e+----+------+
$ F% M5 z; F3 p+ R+ j3 A& O7 f3 rows in set

3 H0 [* M3 e! P  b9 R$ F方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);. f$ F- X3 s9 c: a% l3 X

8 \+ i  i0 U3 w9 D
. u  y9 O, Z7 F5 W& r9 S8 k' F8 C
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表