Operations related to duplicaterecords

Source: Internet
Author: User
Find duplicate data in MySQL, delete duplicate data create table and test data * Table Structure * DROPTABLEIFEXISTS 't1'; CREATETABLEIFNOTEXISTS 't1' ('id' INT (1) NOTNULLAUTO_INCREMENT, 'name' VARCHAR (20) NOTNULL, 'add' VARCHAR (20) NOTNULL, PRIMARYKEY ('id') EngineInno

Find duplicate data in MySQL, delete duplicate data create tables and test data/* Table Structure */DROPTABLEIFEXISTS 't1'; CREATETABLEIFNOTEXISTS 't1' ('id' INT (1) NOTNULL AUTO_INCREMENT, 'name' VARCHAR (20) NOTNULL, 'add' VARCHAR (20) NOTNULL, PRIMARYKEY ('id') Engine = Inno

Find duplicate data in MySQL, delete duplicate data, create tables, and test data

/* Table Structure */
DROPTABLEIFEXISTS 't1 ';
CREATETABLEIFNOTEXISTS 't1 '(
'Id' INT (1) NOTNULL AUTO_INCREMENT,
'Name' VARCHAR (20) NOTNULL,
'Add' VARCHAR (20) NOTNULL,
PRIMARYKEY ('id ')
) Engine = InnoDB;

/* Insert Test Data */
INSERTINTO 't1' ('name', 'add') VALUES
('Abc ',"123"),
('Abc ',"123"),
('Abc ',"321"),
('Abc ',"123"),
('Xzy ',"123"),
('Xzy ',"456"),
('Xzy ',"456"),
('Xzy ',"456"),
('Xzy ',"789"),
('Xzy ',"987"),
('Xzy ',"789"),
('Ijk ',"147"),
('Ijk ',"147"),
('Ijk ',"852"),
('Opq ',"852"),
('Opq ',"963"),
('Opq ',"741"),
('Tpk ',"741"),
('Tpk ',"963"),
('Tpk ',"963"),
('Wher ',"546"),
('Wher ',"546"),
('Server ',"546");

After running the preceding SQL statement, use SELECT * FROM 't1' to test it. To find all duplicate data, only one of them is displayed:
/* Query all duplicate data. The duplicate data is displayed only once */SELECT * FROM 'T1 'group BY 'name' having count (1)> 1 # having count (1) = 1 # This statement indicates that no duplicate data is displayed.

The effect is as follows: id name add
--------------------
1 ABC123
12 ijk 147
15 opq 852
18 tpk 741
21 wer 546
5 xzy 123


Duplicate data display/* search for all duplicate data (duplicate data display */
SELECT t1. * FROM t1, (SELECT t1.name, t1.add FROM t1 group by 'name', 'add' having count (1)> 1) AS 't2 'where 't1 '. 'name' = 't2 '. 'name' AND 't1 '. 'add' = 't2 '. the query result of 'add' is as follows:

Id name add
--------------------
1 ABC123
2 abc 123
4 abc 123
6 xzy 456
7 xzy 456
8 xzy 456
9 xzy 789
11 xzy 789
12 ijk 147
13 ijk 147
19 tpk 963
20 tpk 963
21 wer 546
22 wer 546 shows all data (duplicate data is only displayed once)

The following SQL statements are simple, fast, and effective, but do not guarantee which record is displayed for repeated fields.

SELECT * FROM t1 group by name # having count (*)> 1 # With this statement added, records for repeated fields are displayed only once.
Delete duplicate data: 1. SimplestFastMethod:

1. Create a table with the same table structure and set it to unique through repeated content Fields

2. then execute the statement:Insert ignore into new_table SELECT * FROM old_table

For this question:

1. Create t1_copy and Set name and add as the primary key.

2. Execute insert ignore into t1_copy SELECT * FROM t1 order by t1. 'id', OK

2. Stupid UNION method (large data volume is very slow ):
SELECT     * FROM    t1 GROUP BY `name`,    `add` HAVING COUNT(1) > 1 UNIONSELECT     * FROM    t1 GROUP BY `name`,    `add` HAVING COUNT(1) = 1 ORDER BY id 

Note:

There is a primary key id in the table. If there is no primary key id, it will be slow when the data volume is large, so that a primary key id can be inserted manually. For example

References: Http://www.cnblogs.com/consatan/archive/2010/12/17/1909087.html

Please indicate the source of reprint: http://www.2cto.com/uploadfile/Collfiles/20140712/2014071209024717.

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.