Home > Database > Mysql Tutorial > body text

How to Eliminate Duplicate Rows While Preserving the Original Entry in a Table?

Barbara Streisand
Release: 2024-10-31 19:12:29
Original
720 people have browsed it

How to Eliminate Duplicate Rows While Preserving the Original Entry in a Table?

Eliminating Duplicate Rows While Preserving Originality

Question:

Maintaining data integrity is crucial, and often, duplicate entries can arise from user submissions. In a scenario where duplicate entries exist based on a specific field, such as subscriberEmail, the objective is to eliminate these duplicates, retaining only the original entry. How can this be achieved efficiently without resorting to table swapping?

Answer:

As the table contains unique identifiers for each row, a straightforward approach can be adopted:

<code class="sql">delete x
from myTable x
join myTable z on x.subscriberEmail = z.subscriberEmail
where x.id > z.id</code>
Copy after login

Explanation:

This query leverages the existence of unique IDs to identify the original records. By joining the table against itself using the subscriberEmail field and filtering based on the IDs, it effectively tags duplicate records as those with higher IDs than the original. These tagged duplicates are then deleted.

To further enhance query performance, consider utilizing an index on the subscriberEmail column. This ensures quick retrieval of records based on this field, making the query more efficient. By declaring the subscriberEmail column as a UNIQUE indexed column, it can also help prevent duplicate entries from being inserted in the future.

The above is the detailed content of How to Eliminate Duplicate Rows While Preserving the Original Entry in a Table?. 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