当前位置:首页 > MySQL

Mysql 删除重复记录

canca17年前 (2010-02-06)MySQL573

1、

具体实现如下:

Table

Create Table

------------ --------------------------------------------------------

users_groups CREATE TABLE `users_groups` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`uid` int(11) NOT NULL,

`gid` int(11) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8

users_groups.txt内容:

1,11,502

2,107,502

3,100,503

4,110,501

5,112,501

6,104,502

7,100,502

8,100,501

9,102,501

10,104,502

11,100,502

12,100,501

13,102,501

14,110,501

mysql> load data infile 'c:\\users_groups.txt' into table users_groups fields

terminated by ',' lines terminated by '\n';

Query OK, 14 rows affected (0.05 sec)

Records: 14 Deleted: 0 Skipped: 0 Warnings: 0

mysql> select * from users_groups;

query result(14 records)

id uid gid

1 11 502

2 107 502

3 100 503

4 110 501

5 112 501

6 104 502

7 100 502

8 100 501

9 102 501

10 104 502

11 100 502

12 100 501

13 102 501

14 110 501

14 rows in set (0.00 sec)

根据一位兄弟的建议修改。

mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) >= 1;

Query OK, 7 rows affected (0.11 sec)

Records: 7 Duplicates: 0 Warnings: 0

mysql> truncate table users_groups;

Query OK, 14 rows affected (0.03 sec)

mysql> insert into users_groups select * from tmp_wrap;

Query OK, 7 rows affected (0.03 sec)

Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from users_groups;

query result(7 records)

id uid gid

1 11 502

2 107 502

3 100 503

4 110 501

5 112 501

6 104 502

9 102 501

mysql> drop table tmp_wrap;

Query OK, 0 rows affected (0.05 sec)

2、还有一个很精简的办法。

查找重复的,并且除掉最小的那个。

delete users_groups as a from users_groups as a,

(

select *,min(id) from users_groups group by uid having count(1) > 1

) as b

where a.uid = b.uid and a.id > b.id;

(7 row(s)affected)

(0 ms taken)

3、现在来看一下这两个办法的效率。

很明显的第二个比第一个扫描的函数要少。

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/319.html

标签: MySQL
分享给朋友:

“Mysql 删除重复记录” 的相关文章

小谈MySQL字符集

首先,这片文章纯粹是我的个人经验之谈,适用于我常见的环境及项目中.个人建议,数据库字符集尽量使用utf8(HTML页面对应的是utf-8),以使你的数据能很顺利的实现迁移,因为utf8字符集是目前最适合于实现多种不同字符集之间的转换的...…

mysql 外鍵約束

1. 什么是参照完整性?——————–参照完整性(完整性约束)是数据库设计中的一个重要概念,当数据库中的一个表与一个或多个表进行关联时都会涉及到参照完整性。比如下面这个例子:文章分类表 -  categoriescategory_id ...…

MySql导入SQL文件

MySql导入SQL文件: mysql -u root -p <c:\sampledb.sql…

MySQL建立远程登陆用户

如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server 解决方法: 1. 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要…

mysql配置文件my.cnf详解[部分]

basedir = path 使用给定目录作为根目录(安装目录)。 character-sets-dir = path 给出存放着字符集的目录。 datadir = path 从给定目录读取数据库文件。 pid-file = filename 为mysqld程序指定一个存放进程ID的文件(仅适…

MySQL配置my.cnf调优项详解

MySQL配置调优项详解 以下是一份机器内存:64GB RAM,最大连接数为2000,MySQL使用InnoDB为主的配置说明,某些项的最优值请根据实际生产需要来调.[root@centos190 conf]# cat my.cnf ### MySQL config 5.0/5.1/5.5 ###…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。