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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2016-8-23 20:58:17 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
方法1:
) S& K4 r; l+ q2 p' X6 A! S1、创建一个临时表,选取需要的数据。
- m) {( a! W. T! t) r- ?* ~6 [1 N2、清空原表。& k. O  O8 l# D7 g2 ?6 d
3、临时表数据导入到原表。
& a" {0 s/ A' U4、删除临时表。1 J" f, l5 T' v2 G3 c( i2 q
mysql> select * from student;! P) {9 ^! |. [4 _7 [
+----+------+
( P/ S9 }1 ^* B0 U% X| ID | NAME |+ T" \: O8 U6 u2 m
+----+------+
4 j& Z3 S4 R8 D! a7 p& N| 11 | aa |
1 N$ n7 `8 ?, l/ o| 12 | aa |5 }( @9 ^( d# j; b1 _
| 13 | bb |6 A2 I2 z# c* a/ P" }8 F$ v
| 14 | bb |" J& S7 v* p: X% j- ^! w: N: Z
| 15 | bb |
' M  D" c, K. j9 {5 G| 16 | cc |
) f: ~7 d" R# H# }1 Z1 i# _4 i, Y7 E+----+------+
$ m4 b/ O  S5 n; a/ t8 S6 rows in set
mysql> create temporary table temp as select min(id),name from student group by name;- t% H: t/ O' p
Query OK, 3 rows affected
5 A( l+ _5 {' S/ CRecords: 3 Duplicates: 0 Warnings: 0
mysql> truncate table student;$ @* x2 l9 [3 I- \% e
Query OK, 0 rows affected
mysql> insert into student select * from temp;8 F$ O- S5 H7 \" P
Query OK, 3 rows affected: A1 U" ^- E" z/ B
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;. W3 F2 O1 M+ ^+ L" k! F3 F; N
+----+------+
5 w4 u0 }" c2 `* u| ID | NAME |
1 K7 f0 S+ k( M5 b* [+----+------+
. J7 U6 P9 \" ?$ O. n* w| 11 | aa |
: R/ c" i6 \1 e* h& o; e# X9 h| 13 | bb |
3 \: E1 g) e3 {+ E/ L| 16 | cc |3 f( K3 Q9 F% F% o! f' A4 P& g
+----+------+
& g! S4 U6 f% O& B3 rows in set
mysql> drop temporary table temp;' @+ {( ?: ]4 {% t
Query OK, 0 rows affected* J4 G/ I2 {+ [& Y, S& h) D7 n
这个方法,显然存在效率问题。

方法2:按name分组,把最小的id保存到临时表,删除id不在最小id集合的记录,如下:! s. S( ?4 l: |" x8 [$ g
mysql> create temporary table temp as select min(id) as MINID from student group by name;
9 n8 X) o8 b( j) OQuery OK, 3 rows affected3 N- c' U( B4 E  b: W
Records: 3 Duplicates: 0 Warnings: 0
mysql> delete from student where id not in (select minid from temp);1 D7 R2 z5 o' d3 u9 C- o0 K6 N
Query OK, 3 rows affected
mysql> select * from student;
8 O( Z9 u, t- w# J. q7 Y) @+----+------+
* u# \. v' f4 a! a; c# n9 L  Y+ w| ID | NAME |
  R$ T  y8 ~. q% d+----+------+6 Y! l6 H' I  b! _) m
| 11 | aa |
* z4 ^9 ~" l2 {5 }& |9 v| 13 | bb |
) R. o) H6 [$ x" O8 u. y| 16 | cc |/ n9 v- Q  O9 c) M2 z
+----+------+0 z: G) x! D5 W
3 rows in set

方法3:直接在原表上操作,容易想到的sql语句如下:
mysql> delete from student where id not in (select min(id) from student group by name);# V3 x) E; j; I5 u
执行报错:1093 - You can't specify target table 'student' for update in FROM clause
" m2 S" b& B, x4 L. ^原因是:更新数据时使用了查询,而查询的数据又做了更新的条件,mysql不支持这种方式。# I: h9 H' Z' a4 H; i
怎么规避这个问题?5 h, E/ M4 G, _$ g6 I7 u
再加一层封装,如下:
+ P( `! t5 M# dmysql> delete from student where id not in (select minid from (select min(id) as minid from student group by name) b);
% @7 g0 c+ ^! o% oQuery OK, 3 rows affected
mysql> select * from student;
* I" w% m0 r7 w! e+ ~+----+------+$ B0 ~7 ^! a" s" R% `1 O9 d
| ID | NAME |# k7 V! J( ?3 G
+----+------+
' \4 u$ D6 H8 d' `, z. z' C; M| 11 | aa |
+ l$ Y  [. K" @' ?| 13 | bb |8 Y, D1 |0 y$ [& c, Y6 M# G
| 16 | cc |/ p& _# s+ y8 X9 {( A
+----+------+  i1 G* K7 L; I( C% |/ r
3 rows in set

" m# o$ q$ [  h方法三例如:delete from bugdata where id not in (select minid from (select min(id) as minid from bugdata group by qxbh) b);$ l- T! _' D8 k2 ]; ~
% K# U7 _9 e: q8 M/ _

8 T2 Z1 Y' M6 `2 p! a! e
回复

使用道具 举报

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

本版积分规则

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