msyql deduplication

delete from userinfo where busi_id in
(select busi_id from
(select busi_id from userinfo group by busi_id having count(busi_id)>1) tmp1)
and id not in
(select id from
(select min(id) id from userinfo group by busi_id having count(busi_id)>1) tmp2);


无主键 添加主键再去重操作。


DELETE FROM user WHERE id NOT IN ( SELECT temp.min_id FROM ( SELECT MIN(id) min_id FROM user GROUP BY name,age )AS temp );

If the primary key is not specified in the design table,
possible duplication when importing data import,
resulting in a plurality of recording a plurality of same appear in the table.
The following are ideas to solve this problem:

Use of the distinct statement does not filter out duplicate records stored in the temporary table tmp;
Create Table tmp AS (SELECT distinct SnO, sname, Age, Sex from S);
 
delete data records in the original table
delete from s;
 
the data in the temporary table inserted into the original table.
insert into s select * from tmp;
 
delete the temporary table
drop table tmp; 

 

 

 

 

There is a table of students, the names and gender (0: Female, 1: M), if we want to delete the name and gender are the same data, the method presented here 2

First, look at the data sheet

Numbering Full name gender
1  Wang Lu 0
2  Li Meng 0
3  Zhang Shuai  1 
4   Li Meng   0 
5  Zhang Shuai  1 
6   Liu Tao   1 

NO.1

Id smaller data retention

DELETE t1 FROM student t1, student t2
WHERE t1.name = t2.name AND t1.sex = t2.sex AND t1.id > t2.id

Id larger data retention

DELETE t1 FROM student t1, student t2
WHERE t1.name = t2.name AND t1.sex = t2.sex AND t1.id < t2.id

 

Note that the last of t1.id> t2.id do not forget, otherwise it will delete all the data, be sure to back up your data before operation

 

NO.2

INSERT INTO student_tmp(name, sex)
SELECT DISTINCT name, sex FROM student

 

to sum up

Advantages and disadvantages of the two methods:

Two shorter the time required for the method, but when the second method primary key is UUID, need to deal with it, will be the primary key to increment int, then execute the following sql it.

UPDATE student_tmp SET id = uuid()

Guess you like

Origin www.cnblogs.com/Struts-pring/p/11032918.html