Home > Database > Mysql Tutorial > How to Efficiently Delete Duplicate Rows from a Table Without Creating a New One?

How to Efficiently Delete Duplicate Rows from a Table Without Creating a New One?

Mary-Kate Olsen
Release: 2025-01-02 15:16:39
Original
898 people have browsed it

How to Efficiently Delete Duplicate Rows from a Table Without Creating a New One?

Deleting Duplicate Rows from a Table: An Efficient Approach

Data redundancy within a database can lead to inaccuracies and inefficiencies. Hence, it is essential to remove duplicate records to maintain data integrity. While creating a new table with only distinct entries is an option, we will explore a direct approach to deleting duplicate entries from an existing table without creating a new one.

Consider a table with fields such as id, action, and L11_data. id is unique for every row, L11_data is unique within its respective action field, and action represents industries. The goal is to remove duplicate company names in L11_data for their respective industries.

To achieve this, we can utilize the unique ID field to identify and remove duplicate rows. The following query efficiently eliminates redundant records without creating a new table:

DELETE FROM Table
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM Table
GROUP BY Field1, Field2, Field3, ...
)
Copy after login

In this query, Table represents the existing table, and ID is the unique identifier field. The list of fields specified in GROUP BY includes all fields except for ID.

It's important to note that the efficiency of this query depends on the number of fields and rows in the table. However, for most practical scenarios, it should perform adequately.

Additionally, if you do not have a unique index on the table, it is highly recommended to add one. Not only does this enhance data integrity, but it also enables you to execute the query mentioned above efficiently.

The above is the detailed content of How to Efficiently Delete Duplicate Rows from a Table Without Creating a New One?. 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