方法1:
8 C: c# ?) Z# K2 ~1、创建一个临时表,选取需要的数据。6 {: u' R) Y( F4 ]+ d. G; k) T
2、清空原表。
+ f4 `, m9 o3 X& b* L5 ~& U- n8 e3、临时表数据导入到原表。; E- a# |5 v: b1 h
4、删除临时表。& v0 U$ ]* F) n7 [7 k
mysql> select * from student;+ U9 a4 u$ J7 b
+----+------+1 T/ w: q; c! W
| ID | NAME |, g& p% S7 m; l7 h; p
+----+------+
- ]( M6 D" ?' z% t' e+ k| 11 | aa |7 u f+ x9 l3 G
| 12 | aa |8 y1 O# ]/ Q+ J% } @" N
| 13 | bb |
4 h) M8 h& z9 M+ @| 14 | bb |$ w, P& O) y) l k5 }, a& K( W# B
| 15 | bb |' A* U: c9 a$ O8 V: A _% O
| 16 | cc |* b e# I9 Q/ S7 W' [0 k% x
+----+------+
) t! t7 \; K1 o$ W8 Z# f6 rows in set mysql> create temporary table temp as select min(id),name from student group by name;
/ ]; p2 B6 x1 k9 s% v" ~Query OK, 3 rows affected
+ W4 W3 Y. x" RRecords: 3 Duplicates: 0 Warnings: 0 mysql> truncate table student;
" x7 w$ z- c! K& b( O# KQuery OK, 0 rows affected mysql> insert into student select * from temp;
9 a# k7 X' C, q4 T6 E, cQuery OK, 3 rows affected a1 P& b# K% `
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from student;
; M& q" R) B9 P+----+------+' y. M. [/ k( P! g
| ID | NAME |
8 a$ X8 |" n" M1 ~! q: U8 Z+----+------+% ~# t+ q, y2 l) t8 ^3 R# w, w! p
| 11 | aa |
. H6 |+ i& t, \. g9 S| 13 | bb |- X" ]* Z) F6 X. ~. t/ g
| 16 | cc |/ n3 f8 J6 \8 z$ H7 R. b
+----+------+# |2 T* Z, @% ^( f& I" m
3 rows in set mysql> drop temporary table temp;
: O* g( `8 `4 Y6 c9 T6 n! d+ Q3 jQuery OK, 0 rows affected- k, z# U# }+ \! g
这个方法,显然存在效率问题。
方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:! {5 b7 O9 _3 R9 ?8 x
mysql> create temporary table temp as select min(id) as MINID from student group by name;6 ^6 H! Z$ T2 i* Q
Query OK, 3 rows affected
$ z6 y* l) \8 u+ F+ X% L7 aRecords: 3 Duplicates: 0 Warnings: 0 mysql> delete from student where id not in (select minid from temp);! N; }8 U8 d" L3 ~ J: X0 d
Query OK, 3 rows affected mysql> select * from student;4 t' G, Z$ U9 {2 R
+----+------+5 U" e( G( g2 A% U, M) Y: H% b& d% d
| ID | NAME |# G0 l) E4 V, V8 v! S7 }% ~) [3 H! T
+----+------+
& y3 R; m& Z t- D) B3 _4 p| 11 | aa | F0 U# N& M S: R
| 13 | bb |
# t4 x1 q# P+ E! T| 16 | cc |1 i8 C D* W4 |. v
+----+------+/ L v5 H( T6 U( U
3 rows in set
方法3:直接在原表上操作,容易想到的sql语句如下: mysql> delete from student where id not in (select min(id) from student group by name);5 v( U! ^% z9 Y# D1 |
执行报错:1093 - You can't specify target table 'student' for update in FROM clause5 @, E, r! }3 x& y
原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。- ]8 o' F8 X# L% }1 J
怎么规避这个问题?
' k/ j. y+ \3 C! _# w再加一层封装,如下:
: E& ? v2 h/ D9 ?( `' \( |$ i! j0 ^mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
& ~3 V; H! u4 E5 f" h6 k, L9 KQuery OK, 3 rows affected mysql> select * from student;# q. j& X4 {- M% e6 s; Q# E# o
+----+------+; J. k: D5 b9 A: K
| ID | NAME |
4 Q1 h( t& K& \+----+------+
% s4 s1 ^6 B b. |( F! V. V| 11 | aa |/ P' C+ N; x4 ?6 s
| 13 | bb |5 W P& U$ \+ i
| 16 | cc |
" H P9 B. {3 v+ V6 C+----+------+
% \& P/ ~" y. H7 ]9 f7 I* x3 rows in set * f1 G, {$ w4 f
方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);9 z5 [3 z; L4 i' Q3 C# n/ ?" f
' B. a0 \6 U; H ?, z. r: y/ i/ } G; ` |; P/ L D+ f
|