方法1:" F- A4 ~! D+ Y$ y. l
1、创建一个临时表,选取需要的数据。$ J% Z( t* m. R v t( j* o0 p
2、清空原表。& ^3 g) A) f; D
3、临时表数据导入到原表。
; w/ Q3 l' ~& |9 j# v$ S4、删除临时表。3 A6 C! p" A0 B/ }3 f# K; T
mysql> select * from student;
9 _$ n j. ~% N) K) h+----+------+ I' b/ [, e3 E) H/ i% A5 j
| ID | NAME |
0 l* E* ^! L% ~ I/ D+----+------+8 ~( T% k# N5 f, n0 m- H
| 11 | aa |/ I" N2 C. t1 D, l2 s
| 12 | aa |4 g0 B/ T7 Z0 Y! R
| 13 | bb |
# D6 `/ _: w- o5 b; [' T| 14 | bb |
' A- j* B2 e; \) i, a4 B| 15 | bb |) r5 V I8 g/ p# s
| 16 | cc |
+ Z9 z, L5 @2 ~8 L0 j( [% F+----+------+- f% r c0 Z/ G/ `
6 rows in set mysql> create temporary table temp as select min(id),name from student group by name;
6 E5 }& B3 i* v% RQuery OK, 3 rows affected
) J! n8 ]; V: z* NRecords: 3 Duplicates: 0 Warnings: 0 mysql> truncate table student;
n" }. X9 A+ ^7 e1 W. AQuery OK, 0 rows affected mysql> insert into student select * from temp;
7 d; b" {, }* m. y7 pQuery OK, 3 rows affected
/ W z* I' r5 k2 m' YRecords: 3 Duplicates: 0 Warnings: 0 mysql> select * from student;* U: N4 D7 }) C8 d6 h& r, B
+----+------+
8 L4 `1 z9 d# x| ID | NAME |3 M/ u n) y& v7 v
+----+------+
" a! g: k V+ a6 Z0 l9 q| 11 | aa |
, ?, Y. j& e; D5 o2 ]# ^0 m+ Y' t| 13 | bb |
/ j; K7 ~5 Y* C0 w& P/ `- V# ?0 [, M| 16 | cc |# W3 U7 v. r; n3 Q+ _1 h/ ?
+----+------+! Q2 m, W7 c" a
3 rows in set mysql> drop temporary table temp;' ?' v+ H! D, `* H
Query OK, 0 rows affected$ K m! j- y. s q2 s" S& L c
这个方法,显然存在效率问题。
方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:5 }" }; z3 |; j d( Q( @
mysql> create temporary table temp as select min(id) as MINID from student group by name;
5 @. @7 T& O" f2 EQuery OK, 3 rows affected+ o& h& Y8 o% e7 u& k3 ^
Records: 3 Duplicates: 0 Warnings: 0 mysql> delete from student where id not in (select minid from temp);
4 M7 |+ Y4 h' ~" a8 fQuery OK, 3 rows affected mysql> select * from student;
4 n4 G0 I. K: [% n$ I! y+----+------+: G. F* {8 q0 {) u
| ID | NAME |5 J" S7 T9 d, ]7 ~; y( t
+----+------+9 |* _( }6 z+ ]
| 11 | aa |
8 v/ P- F( ^+ P3 j+ N" }| 13 | bb |& B$ b( ]0 D! t, y# k
| 16 | cc |5 j+ h5 |6 G ?0 @( n
+----+------+
1 O1 {# H! r. D9 o3 rows in set
方法3:直接在原表上操作,容易想到的sql语句如下: mysql> delete from student where id not in (select min(id) from student group by name);! l0 P+ m6 D+ y& m3 P3 m
执行报错:1093 - You can't specify target table 'student' for update in FROM clause9 B6 ~" A! C8 E
原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
/ p& j1 H7 G- L2 c怎么规避这个问题?
- ]5 O! x4 ^3 }7 ]1 M) J再加一层封装,如下:( L, l0 }$ I# R) H j: [$ \
mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);- q% Z: z( L9 Q) e# _- P
Query OK, 3 rows affected mysql> select * from student;# o( y/ w- e( _- |
+----+------+5 g4 Y" R! F' w {6 y. E1 O7 }2 g
| ID | NAME |% R# u$ E7 i: J1 G9 a
+----+------+
" ~. n( @' u. J$ C6 l" Z) d| 11 | aa |: r( I5 i9 _$ q c
| 13 | bb |; G2 m- K; Z% w0 z. R2 b
| 16 | cc |/ t7 o2 n" v; }9 p: L' W1 |) |
+----+------+. ?; g5 q' f4 ?2 N
3 rows in set
8 z. S/ J) j; V' |0 `- v8 A方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);; q5 ^' E! p" c0 A& Z1 W. L9 U( t0 l
2 J ~; R( K8 [5 v8 b I |' ^: L
% s9 l! V% l. j. S% | p. Z, A
|