Home > Database > Mysql Tutorial > How to Efficiently Delete Duplicate Rows in MySQL While Keeping One Record?

How to Efficiently Delete Duplicate Rows in MySQL While Keeping One Record?

Barbara Streisand
Release: 2025-01-23 13:21:10
Original
580 people have browsed it

How to Efficiently Delete Duplicate Rows in MySQL While Keeping One Record?

Delete duplicate rows (duplicate data) in MySQL

Question:

How to remove duplicate rows from a MySQL table while retaining a single record for each unique value?

Example:

Consider the following data in the names table:

id name
1 google
2 yahoo
3 msn
4 google
5 google
6 yahoo

Solution:

Warning: Always take the precaution of performing this operation on a test copy of the table first.

To remove all duplicate rows (keep one) and keep the record with the lowest or highest id value, use the following query:

Keep the lowest id value:

<code class="language-sql">DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name</code>
Copy after login

Keep the highest id value:

<code class="language-sql">DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name</code>
Copy after login

Additional notes:

  • Make sure to add the condition AND n1.id != n2.id to both queries to prevent accidentally deleting all rows.
  • This method is computationally expensive and may break your connection if the table is large.
  • For large tables, consider using INSERT with DISTINCT as a faster alternative:
<code class="language-sql">INSERT INTO tempTableName(cellId,attributeId,entityRowId,value)
    SELECT DISTINCT cellId,attributeId,entityRowId,value
    FROM tableName;</code>
Copy after login

The above is the detailed content of How to Efficiently Delete Duplicate Rows in MySQL While Keeping One Record?. For more information, please follow other related articles on the PHP Chinese website!

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 admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template