Home > Database > Mysql Tutorial > body text

How to implement duplication checking in mysql and only leave one

藏色散人
Release: 2020-11-01 14:33:16
Original
2373 people have browsed it

Mysql method to implement duplicate checking and keep only one: first find the redundant duplicate records in the table through "select * from"; then delete the duplicate data through "delete from" and keep only one data.

How to implement duplication checking in mysql and only leave one

Recommended: "mysql video tutorial"

mysql deletes duplicate data and keeps only one record

Delete duplicate data and keep the record with the smallest id in name

delete from order_info where id not in (select id from (select min(id) as id from order_info group by order_number) as b);
Copy after login

delete from table where id not in (select min(id) from table group by name having count(name)>1) and  id in (select id group by name having count(name)>1)
Copy after login

(Note: The way the HAVING clause sets conditions for the GROUP BY clause is similar to the way WHERE and SELECT interact. WHERE Search conditions are applied before the grouping operation is performed; HAVING search conditions are applied after the grouping operation is performed. The HAVING syntax is similar to the WHERE syntax, but HAVING can contain aggregate functions. The HAVING clause can reference any item displayed in the select list.)

Extension:

SQL: Delete duplicate data and keep only one SQL statement. Delete duplicates and keep only one among thousands of records. , there are some identical records, how can we use SQL statements to delete duplicates?

1. Find the redundant duplicate records in the table. Duplicate records are based on a single field (peopleId) To judge

select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
Copy after login

2. Delete redundant duplicate records in the table. Duplicate records are judged based on a single field (peopleId), leaving only the record with the smallest rowid

delete from people where   peopleName in (select peopleName    from people group by peopleName      having count(peopleName) > 1) and   peopleId not in (select min(peopleId) from people group by peopleName     having count(peopleName)>1)
Copy after login

3. Look up the table Redundant duplicate records (multiple fields)

select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
Copy after login

4. Delete redundant duplicate records (multiple fields) in the table, leaving only the record with the smallest rowid

delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
Copy after login

5. Look up the table Redundant duplicate records (multiple fields), excluding the record with the smallest rowid

select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
Copy after login

6. Eliminate the first digit on the left of a field:

update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'
Copy after login

7. Eliminate the first digit on the right of a field First place:

update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'
Copy after login

8. Fakely delete redundant duplicate records (multiple fields) in the table, excluding the record with the smallest rowid

update vitae set ispass=-1 where peopleId in (select peopleId from vitae group by peopleId,seq having count(*) > 1) and seq in (select seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
Copy after login

The above is the detailed content of How to implement duplication checking in mysql and only leave one. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!