MYSQL中删除重复记录的方法
在实际应用中,很可能会碰到一些需要删除某些字段的重复记录,我现在把我能想到的写下来,望高手们补充。
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 |
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|