当前位置:首页 > MySQL > 正文内容

Mysql 删除重复记录

canca16年前 (2010-02-06)MySQL465

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 配置命令大全

--auto-rehash       Enable automatic rehashing. One doesn't need to use             ...

MySql导入SQL文件

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

MySQl字段类型

MySQL支持大量的列类型,它可以被分为3类:数字类型、日期和时间类型以及字符串(字符)类型。本节首先给出可用类型的一个概述,并且总结每个列类型的存储需求,然后提供每个类中的类型性质的更详细的描述。概述有意简化,更详细的说明应该考虑到有关特定列类型的附加信息,例如你能为其指定值的允许格式。 由MyS...

MySQL建立远程登陆用户

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

MySQL备份与恢复语句

mysql备份语句:E:\MySQL Server 5.0\bin>mysqldump -uroot -padmin --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob -x s...

发表评论

访客

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