找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2532|回复: 0
打印 上一主题 下一主题

mysql删除重复记录,保存Id最小的一条

[复制链接]
跳转到指定楼层
楼主
发表于 2016-8-23 20:58:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
方法1:: ]* K/ b. D4 d+ S3 p
1、创建一个临时表,选取需要的数据。1 n3 @7 U- J  e! R! x
2、清空原表。
2 Y1 Q2 `1 _! M6 P3、临时表数据导入到原表。
5 M, k! o7 C0 a3 i# w. X8 O4、删除临时表。& }4 O& z0 `/ ]! c2 |. _; V2 z
mysql> select * from student;5 L8 @2 {% P; k$ `  C% Y
+----+------+0 s0 [  U$ i; k  T5 r
| ID | NAME |
( g; H) ?1 E4 B9 c3 z0 N2 A+----+------+% S8 i: }, G- L0 L# |0 I
| 11 | aa |
; _7 B  _9 B5 x; {2 p5 ^/ ]5 n| 12 | aa |- O0 \! e. S( H( S) T
| 13 | bb |
. k5 z7 v# |. J  H$ F| 14 | bb |& l; O# \; c3 h' j. w, `
| 15 | bb |
. [, o5 i  C; x/ `9 J| 16 | cc |
. n  S3 N" c0 m% i, Q+----+------+5 v/ b. L3 a6 b
6 rows in set
mysql> create temporary table temp as select min(id),name from student group by name;7 r- ?8 T) z9 X, W. [
Query OK, 3 rows affected
. M$ a* w- |4 N9 E& WRecords: 3 Duplicates: 0 Warnings: 0
mysql> truncate table student;5 \$ K6 y+ ^8 t5 Z" B' F
Query OK, 0 rows affected
mysql> insert into student select * from temp;
- f. ~: g  y  d1 g& uQuery OK, 3 rows affected
" T# L" x2 z0 w- S* B5 hRecords: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
7 G# ?$ K* R0 D+----+------+) a% {+ K) s7 }8 t6 Z* G: |& E- r
| ID | NAME |
- z4 ?; x& x1 [1 D+----+------+
# P) F9 b" l9 M* @  Y| 11 | aa |
+ A0 }2 }' Z8 P| 13 | bb |
2 B4 [# P& x5 @9 w' c3 r, Q  {| 16 | cc |; `: c: K3 r' |; i$ k, i) A
+----+------+( `, f0 B2 ?. R" d) V# ]* ^2 [$ U
3 rows in set
mysql> drop temporary table temp;. N7 L: s% W, k. ?( S3 B" Q. |/ x
Query OK, 0 rows affected* x9 s' \& @7 h5 u
这个方法,显然存在效率问题。

方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:; X+ Q9 g% ~" I' c  a- q
mysql> create temporary table temp as select min(id) as MINID from student group by name;2 F/ i/ d5 W* a+ ^
Query OK, 3 rows affected& K: @8 E( N, R' Y, z" _
Records: 3 Duplicates: 0 Warnings: 0
mysql> delete from student where id not in (select minid from temp);
4 L1 N$ w" }. D9 g: a8 W9 \; A* MQuery OK, 3 rows affected
mysql> select * from student;8 x. e0 v$ Y: p" P! @
+----+------+
7 e1 c6 u& T$ V- x$ f" \/ H| ID | NAME |- L: f  r7 n' O: @1 n3 w
+----+------+
0 M* X+ I9 |3 i6 `| 11 | aa |3 a8 B" _& m7 C1 h# {% Q" x7 M
| 13 | bb |
4 r8 Q5 ]8 p% g1 P/ {/ N: y( f| 16 | cc |3 y& T4 |8 u4 Q: ?! ?
+----+------+1 ^( e' r/ L9 R
3 rows in set

方法3:直接在原表上操作,容易想到的sql语句如下:
mysql> delete from student where id not in (select min(id) from student group by name);$ Y6 [  S# K' }/ Y* V
执行报错:1093 - You can't specify target table 'student' for update in FROM clause' v; U7 N6 d8 F& n- w6 u! j
原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。
) I, C, b' L& P$ I" u1 G6 p, p; v怎么规避这个问题?* ^  p3 q3 w5 \6 A0 G* ^/ q0 A
再加一层封装,如下:  l( z6 B* S! \: r
mysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);( `; E. @! r" \9 s. P" Z% R
Query OK, 3 rows affected
mysql> select * from student;  {3 `5 c& I7 L# E/ }
+----+------+
6 ^. u1 c! Q  k" m& q$ X| ID | NAME |
4 t0 i  ^8 K- u+----+------+' {) t) P" y& W- w2 w( O
| 11 | aa |3 s( a+ s1 c1 d8 b# }
| 13 | bb |+ t2 p9 a+ |8 T1 c- P
| 16 | cc |
" ~; w. Q" O5 ]$ b" A- T+----+------+. D1 z  u* o4 \. q/ z
3 rows in set
+ p, s' j/ V& W( F' a0 W( s
方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);
! F0 ~. `5 B$ b9 T& Q& y  G, y4 D8 j( Z! k8 e

+ t8 J2 l  E! c2 t1 i) r& a7 c
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表