|
方法1:
& E$ I; k+ r2 J1、创建一个临时表,选取需要的数据。
0 Z0 z$ Z1 Z3 y0 a: I2、清空原表。
5 M- J9 W" r) q8 D. s- A5 u3、临时表数据导入到原表。8 i2 ?1 W( A$ p) ~
4、删除临时表。
/ d' f- Y& B Lmysql> select * from student;; D- n% D% W2 ?, c# W
+----+------+
. | i) K+ }5 A9 y| ID | NAME |! G1 E$ V( N7 ]9 \
+----+------++ M ^3 N# p- v& W7 O8 S
| 11 | aa |: E2 R' W' t' j4 w* \; h- k5 e, A
| 12 | aa |
6 g% r6 b3 `* Q| 13 | bb |
8 a( n! L- [7 x f7 `| 14 | bb |$ r4 s' |" p0 W I9 e
| 15 | bb |
$ M6 U5 v" a, |$ m& U# e| 16 | cc |
7 w! a( c$ k h+----+------+' E* }% }$ Z- p8 B4 A) Y; w) c6 R
6 rows in set mysql> create temporary table temp as select min(id),name from student group by name;
! |& Z6 J, Q$ r/ D9 ?7 iQuery OK, 3 rows affected3 G4 X# @" }+ {* ?: T$ Z. }
Records: 3 Duplicates: 0 Warnings: 0 mysql> truncate table student;
& A* N) \: m9 p4 i7 `6 A2 r7 }& pQuery OK, 0 rows affected mysql> insert into student select * from temp;) U2 U$ G; L8 R- K$ S
Query OK, 3 rows affected
& S _- S2 { s5 I$ h! FRecords: 3 Duplicates: 0 Warnings: 0 mysql> select * from student;
2 L! ]) r4 ^: _& Q) T4 ?+----+------+7 r2 y' B1 [0 w$ p# K( Y7 d- L
| ID | NAME |
6 {( e4 y. d4 a' m1 O* E+----+------+' Y) u: `( u% @$ T
| 11 | aa |
* K6 A4 B6 a( c0 n2 D; D) G| 13 | bb |
\6 Z9 e: o3 W6 H! }| 16 | cc | _8 I7 p9 J# R' V/ z
+----+------++ M$ \; a) `9 h/ k7 u
3 rows in set mysql> drop temporary table temp;, z# A. \. [7 o' Z& ` n
Query OK, 0 rows affected7 K; @! v& E7 E; }6 ~
这个方法,显然存在效率问题。
方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:$ o' X8 [. g6 [& A& t
mysql> create temporary table temp as select min(id) as MINID from student group by name;* v# o: g D/ ?- M" R
Query OK, 3 rows affected
) F7 V, I- D# TRecords: 3 Duplicates: 0 Warnings: 0 mysql> delete from student where id not in (select minid from temp);
* Z7 D4 O7 f' T5 @* ~3 C' ~Query OK, 3 rows affected mysql> select * from student;- [5 M k% l( i8 V, t
+----+------+4 u% x; H. ^5 L5 `' l
| ID | NAME |# X" {$ T7 h( c; ]
+----+------+; W, K9 T5 a# s6 w' k
| 11 | aa |
! g: K$ y. Q& q7 K| 13 | bb |: c; ^2 ~% p* d0 N+ s9 U
| 16 | cc |: }5 P# V* S* z; m4 B# a- E$ u
+----+------+
* L3 w2 h* o5 k; F) A2 p& X7 B3 rows in set
方法3:直接在原表上操作,容易想到的sql语句如下: mysql> delete from student where id not in (select min(id) from student group by name);
2 F5 A: i D! h& V执行报错:1093 - You can't specify target table 'student' for update in FROM clause
* X) @4 A! ]6 M! M u原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
$ V$ F G; B$ {: H& s怎么规避这个问题?
! W3 L6 U6 z N9 m! e; _再加一层封装,如下:2 Y+ q& j$ M( E: y
mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);' Y* t! H6 H% P/ g& I
Query OK, 3 rows affected mysql> select * from student;
' C" {2 a6 h5 `+----+------+8 Q9 ?$ d. ^) H. k4 F7 o- [
| ID | NAME |
* W0 e4 P: e; x& k+ f+----+------+
( c, C' c- `; I" L| 11 | aa |( O1 a9 Q3 x9 M9 _( P; X! r
| 13 | bb |* K, K' r g: g
| 16 | cc |0 H' f F) h9 c: J% f
+----+------+' L! q8 F/ W" M. M
3 rows in set + v" u+ \9 l1 a6 s7 ^7 f' L" M
方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);0 o8 Y# o. c, V
1 S9 l) x+ f8 c, i5 ]3 n8 x
/ c1 M1 T, R9 V% n |