方法1:3 R3 n: g) |; \) I) X4 a- g
1、创建一个临时表,选取需要的数据。
+ r- W* y1 t0 |& S; i# G! G2、清空原表。
% ^: s5 o3 j% Y$ K4 y3、临时表数据导入到原表。
# w; ^* r. ?6 }7 Q+ z- ~4、删除临时表。
. ?( n A6 p% b3 G( Imysql> select * from student;$ w/ A! N5 t7 [8 t" O
+----+------+; j$ U9 [6 t. R6 A8 e# ]) A# \
| ID | NAME |1 u3 m: f' X6 U9 s
+----+------+8 M6 f z, R( `7 U% d
| 11 | aa |2 a, m* I0 y) g! w" L
| 12 | aa |
6 c" K7 W' k& a* d' y; @| 13 | bb |
; N" p3 k7 M! k1 d| 14 | bb |5 l) _" L, o$ R+ R) a% h
| 15 | bb |" v* C2 I& p7 X2 F4 I
| 16 | cc |
) z0 ` @! [$ e' i7 V+----+------+
9 g+ Z2 G& O& t) x8 Q3 |6 rows in set mysql> create temporary table temp as select min(id),name from student group by name;
, b$ Q4 j; D0 k r+ f9 |4 xQuery OK, 3 rows affected
7 {( l8 A; v9 Z9 i4 N! c. n( _Records: 3 Duplicates: 0 Warnings: 0 mysql> truncate table student;
$ G# F. M$ b5 U: O5 B1 bQuery OK, 0 rows affected mysql> insert into student select * from temp;
( e% b. Y8 `' U5 xQuery OK, 3 rows affected; q6 i' Y# A$ I* l! d/ r2 S
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from student;
! N5 w+ h2 m% } W- Q! _7 x+----+------+
/ O( M. Q% u% X( e| ID | NAME |
7 M9 B+ p- q/ Z# C. q& w+----+------+; e/ c7 i3 n' H5 X, O) V" i
| 11 | aa |# o: m( n# n" e: u t b
| 13 | bb |
: j3 I; |+ @' E) W* ]+ k: [. E; J| 16 | cc |
* l- \& q/ w( ~$ ]+----+------+
& o: q4 @: y( U( ]3 rows in set mysql> drop temporary table temp;
1 d( l2 k# {3 o/ PQuery OK, 0 rows affected
! L6 M/ n; m0 u4 t9 }5 V8 C这个方法,显然存在效率问题。
方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:
$ c \& R6 I' p+ m" Xmysql> create temporary table temp as select min(id) as MINID from student group by name;
6 p! A. W2 `. |' b! T! o6 YQuery OK, 3 rows affected1 Y. i0 V# o/ D: w9 B) M
Records: 3 Duplicates: 0 Warnings: 0 mysql> delete from student where id not in (select minid from temp);8 k% t j" m$ E
Query OK, 3 rows affected mysql> select * from student;" g0 n+ i) }8 m+ }6 @ z
+----+------+$ }6 U! I2 y. x" q. t
| ID | NAME |' Y$ A1 s) @2 A" t G; Y, u8 L
+----+------+% K% g, [1 a6 i% u# }; P) d
| 11 | aa |! T7 g1 l( h3 B5 W+ k+ Z& M
| 13 | bb |
4 M7 I. l. W e8 F| 16 | cc |( f; P0 _; s' C
+----+------+% E0 q( {. ^* A3 b; n# B4 w5 d
3 rows in set
方法3:直接在原表上操作,容易想到的sql语句如下: mysql> delete from student where id not in (select min(id) from student group by name);
) p: O0 k% F- b( c执行报错:1093 - You can't specify target table 'student' for update in FROM clause G* G$ r! ^1 Y# N
原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
, t0 E4 p$ k( h" B$ Z2 ^怎么规避这个问题?3 p% @0 \) R; E4 o4 k: d* @
再加一层封装,如下:
. h. s G: ^7 }7 Tmysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
9 M! k9 T6 O, ~) ~Query OK, 3 rows affected mysql> select * from student;
* L5 R5 @. W/ ]7 S# d: J+----+------+
/ w6 ]2 w3 V. {/ m! F; D| ID | NAME |% w1 ~7 k# W. w
+----+------+) C, ]# Z) J* V" J) u! B5 ^
| 11 | aa |
4 u+ \- q: ?. y- E1 B| 13 | bb |
) M1 r \2 U- ~% T| 16 | cc |6 Q8 {+ j; c3 C0 \& m
+----+------+" _1 X# C, \* l$ w k- h* J
3 rows in set
. n( b( @ o$ }/ k/ B1 {+ H方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);+ Q7 `. @& c6 e6 I1 U" d/ p ?
: |) {- |: u' E* D
& V1 T3 B ?* a0 ?+ _3 r |