SQL statement for deleting duplicate records in mysql

Source: Internet
Author: User
In SQL, we often encounter repeated data. Below I will introduce multiple methods for deleting Repeated Records in mysql. If you need it, please refer to it.

In SQL, we often encounter repeated data. Below I will introduce multiple methods for deleting Repeated Records in mysql. If you need it, please refer to it.

Method 1:

1. Create a temporary table

The Code is as follows:

Create table tmp as select * from youtable group by name (name is a column that does not need to be duplicated)

2. Delete the original table

The Code is as follows:

Drop table youtable

3. rename a table

The Code is as follows:

Alter table tmp rename youtable

However, this method has a problem. The structure of the final table transformed from a temporary table is inconsistent with that of the original table and needs to be changed manually. This problem is to be solved.
Method 2:

1. Create a temporary table

The Code is as follows:

Create table tmp as select * FROM youtable group by name (name is a column that does not need to be duplicated)

2. Clear the original table

The Code is as follows:

Truncate table youtable

3. Insert a temporary table to youtable

The Code is as follows:

Insert into tablename SELECT * FROM temp

4. delete a temporary table

The Code is as follows:

Drop table temp

Method 3:

The Code is as follows:

Delete table where ID not in (select min (ID) from table group by name (name: Repeated field ))

Method 4:


The specific implementation is as follows:

The Code is as follows:

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 content:

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)

Modify according to a brother's suggestion.

The Code is as follows:

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. There is also a very streamlined approach.

Find the duplicate and remove the smallest one.

The Code is as follows:

Delete users_groups as a from users_groups as,
(
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)

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

3. Let's take a look at the efficiency of the two methods.
Run the following SQL statement:

The Code is as follows:

Create index f_uid on users_groups (uid );
Explain select * from users_groups group by uid having count (1)> 1 union all
Select * from users_groups group by uid having count (1) = 1;
Explain select * from users_groups as,
(
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;
Query result (3 records)
Id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY users_groups index (NULL) f_uid 4 (NULL) 14
2 UNION users_groups index (NULL) f_uid 4 (NULL) 14
(NULL) UNION RESULT ALL (NULL)


Query result (3 records)
Id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY ALL (NULL) 4
1 PRIMARY a ref PRIMARY, f_uid 4 B. uid 1 Using where
2 DERIVED users_groups index (NULL) f_uid 4 (NULL) 14



Obviously, the second one is less than the first scan function.

If you do not have the permission to create a table or create an index

Create a new table and insert the data in the original table into the new table:

The Code is as follows:

Mysql> create table demo_new as select * from demo group by site;
Query OK, 3 rows affected (0.19 sec)
Records: 3 Duplicates: 0 Warnings: 0

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| Demo |
| Demo_new |
+ ---------------- +
2 rows in set (0.00 sec)

Mysql> select * from demo order by id;
+ ---- + ------------------------ +
| Id | site |
+ ---- + ------------------------ +
| 1 | http://www.hzhuti.com |
| 2 | http://111cn.net |
| 3 | http://www.111cn.net |
| 4 | http://www.hzhuti.com |
| 5 | http://www.111cn.net |
+ ---- + ------------------------ +
5 rows in set (0.00 sec)

Mysql> select * from demo_new order by id;
+ ---- + ------------------------ +
| Id | site |
+ ---- + ------------------------ +
| 1 | http://www.hzhuti.com |
| 2 | http://111cn.net |
| 3 | http://www.111cn.net |
+ ---- + ------------------------ +
3 rows in set (0.00 sec)

Back up the original table and rename the new table to the current table:

The Code is as follows:

Mysql> rename table demo to demo_old, demo_new to demo;
Query OK, 0 rows affected (0.04 sec)

Mysql> show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| Demo |
| Demo_old |
+ ---------------- +
2 rows in set (0.00 sec)

Mysql> select * from demo order by id;
+ ---- + ------------------------ +
| Id | site |
+ ---- + ------------------------ +
| 1 | http://www.hzhuti.com |
| 2 | http://111cn.net |
| 3 | http://www.111cn.net |
+ ---- + ------------------------ +
3 rows in set (0.00 sec)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.