How to implement a MySQL query to remove duplicate data and keep only one record.

To search for duplicate data and keep only one unique record, you can follow the steps below:

  1. Use the GROUP BY clause and aggregate functions to identify duplicate data and only retain one record.
  2. Filter out duplicate records using the HAVING clause.
  3. Use subqueries or temporary tables to store duplicate records, and then delete these duplicate records using primary keys or unique indexes.

Here is an example SQL query to remove duplicate data and keep only one record:

SELECT MIN(id), column1, column2, column3
FROM your_table
GROUP BY column1, column2, column3
HAVING COUNT(*) > 1;

In the example above, assuming your_table is a table with duplicate data, where column1, column2, and column3 are the duplicate columns, this SQL statement will return the minimum id value for each group of duplicate data and keep one unique record.

You can also insert the query results into a temporary table, remove duplicate data from the original table, and then reinsert the data from the temporary table back into the original table. This can achieve the effect of deleting duplicate data and keeping only one record.

广告
Closing in 10 seconds
bannerAds