Home  >  Article  >  Database  >  How to Delete Duplicate Rows While Keeping the Oldest Submission?

How to Delete Duplicate Rows While Keeping the Oldest Submission?

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 05:54:27271browse

 How to Delete Duplicate Rows While Keeping the Oldest Submission?

Managing Duplicate Rows: Preserving Oldest Submissions

Duplicate data can significantly impact the integrity and usability of any database. In this scenario, our goal is to eliminate duplicate rows based on the subscriberEmail field, retaining only the original submission.

To achieve this without resorting to table swapping techniques, we can employ the following SQL query:

delete x 
from myTable x
join myTable z on x.subscriberEmail = z.subscriberEmail
where x.id > z.id

This query uses the power of table aliasing. By creating aliases x and z for the myTable table, we can compare rows within the same table. Specifically, we join x and z on the subscriberEmail field, effectively creating a self-join.

The where clause performs the crucial filtering. It eliminates records in x where the id field is greater than the corresponding id field in z. This ensures that only non-original (duplicate) submissions are targeted for deletion.

To further enhance performance, consider implementing a UNIQUE index on the subscriberEmail column. This will automatically prevent duplicate entries from being inserted into your table in the future.

By implementing this query, you can efficiently remove duplicate rows, preserving the oldest submission for each email address without the need for complex table manipulations or data shuffling procedures.

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

Statement:
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