中国网络渗透测试联盟

标题: mysql删除重复记录,保存Id最小的一条 [打印本页]

作者: admin    时间: 2016-8-23 20:58
标题: mysql删除重复记录,保存Id最小的一条
方法1:
7 N5 ]# v; m( n2 N' o/ y2 a1、创建一个临时表,选取需要的数据。3 Y3 `3 K7 g; R
2、清空原表。4 V# S# J! o) y5 h' C9 u
3、临时表数据导入到原表。3 D& m6 d# h- W& b: P% G: o2 _
4、删除临时表。
" }. _3 n% ]- j* r2 a' G) n& Smysql> select * from student;7 U1 S  ^' w* O3 H& K2 D* Y$ I* g
+----+------+
' l& I  @8 ^; x6 w$ C| ID | NAME |' @1 j6 E( G; c6 G1 s( a% F
+----+------+9 P4 ?: d# U  y3 U- V- b3 Z7 x& _; r
| 11 | aa |5 V) w" t: {' ~3 x4 |
| 12 | aa |
0 e0 @8 i! q7 {| 13 | bb |% O& C9 L  K! o# E2 Q6 n
| 14 | bb |4 V2 }8 J( @% C% q% R- n* N3 [
| 15 | bb |
$ T6 R, c6 v" F: }# `( || 16 | cc |
$ X/ H$ q. `  W$ l, l% N+----+------+. o+ i% E; P1 X& R
6 rows in set
mysql> create temporary table temp as select min(id),name from student group by name;
/ U% {. Y/ V! _Query OK, 3 rows affected+ z' A! m1 e( L9 ^* M4 J, n. u  T
Records: 3 Duplicates: 0 Warnings: 0
mysql> truncate table student;0 q- C) A2 ~) Q0 n. [3 s( Z5 K
Query OK, 0 rows affected
mysql> insert into student select * from temp;! |+ ^& ~: }) T! \
Query OK, 3 rows affected- O) A  ~4 k4 G! k( O* @
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
( c3 ?3 M! i$ r; p+----+------+
: U# n/ @) x0 N4 u+ x| ID | NAME |1 f$ h2 [9 B/ z* l6 x6 q9 C
+----+------+( O, Q2 y8 l' c
| 11 | aa |; N5 }8 y5 i. Y0 V$ k
| 13 | bb |6 _7 V% L  b# r. X- H9 c5 I+ n
| 16 | cc |
% j& H/ s( {6 H. m( o+----+------+/ X, E" {% _+ J/ t. q" y
3 rows in set
mysql> drop temporary table temp;
: N- [+ q5 h7 M: h: \! XQuery OK, 0 rows affected, d0 y" v  `" f* E( G3 \/ T/ K! \
这个方法,显然存在效率问题。

方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:; n3 a/ e  V2 ?0 |, Y) Y
mysql> create temporary table temp as select min(id) as MINID from student group by name;' }' |) @( Q6 C0 G
Query OK, 3 rows affected- ]+ F4 K6 \. X- q- v9 s$ ~
Records: 3 Duplicates: 0 Warnings: 0
mysql> delete from student where id not in (select minid from temp);& P% x9 u/ ^: J
Query OK, 3 rows affected
mysql> select * from student;
2 {7 a$ M* h" U% d+----+------+
# u7 k: s: ]  e; q( c7 Y* z| ID | NAME |5 e3 s1 s. i( [. B8 n
+----+------+
  R- e9 I& c7 _0 q) I| 11 | aa |! m/ z9 G& T+ }. i
| 13 | bb |' V5 S6 V$ |% X8 r! e- h) [! q; H
| 16 | cc |
( t! M; t/ U0 V& W( M- w+----+------+
. `# A7 b$ S$ h' A! m3 rows in set

方法3:直接在原表上操作,容易想到的sql语句如下:
mysql> delete from student where id not in (select min(id) from student group by name);4 P8 i2 o9 Z- E* p) ~
执行报错:1093 - You can't specify target table 'student' for update in FROM clause
( B& `, j  a+ w原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
" ~% |3 a$ W( L怎么规避这个问题?9 s0 ^& w( [- i, g/ p
再加一层封装,如下:; |1 G9 F# L! Y! F1 i. z3 ]) C
mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
9 g* H4 A8 J  @8 J- d0 D. s  g% ?Query OK, 3 rows affected
mysql> select * from student;
! M. Y# D6 `9 C+----+------+
, k7 y3 X) M; O| ID | NAME |" Y% u( v0 {- F, _8 i8 u) p
+----+------+
2 g! M& J* a, {+ y* J| 11 | aa |
7 a" y0 [0 n2 b$ W| 13 | bb |& b4 b2 V1 b# h/ H
| 16 | cc |) T6 p$ U8 t' M) Q9 J
+----+------+
$ f+ f- M& {! ?  R3 O3 rows in set
# p4 l9 v" j. p/ J- d, Z
方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);) |; J) ~* I/ Z6 T
* |6 \6 \( H! c. g
+ ^2 {) i1 F* o0 H, y. |! i





欢迎光临 中国网络渗透测试联盟 (https://cobjon.com/) Powered by Discuz! X3.2