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

How to Delete Duplicate Rows While Keeping the Oldest Submission?

Linda Hamilton
Release: 2024-11-01 05:54:27
Original
345 people have browsed it

 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
Copy after login

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!

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