How to delete duplicate records in mysql

PHPz
Release: 2023-04-19 17:22:51
Original
774 people have browsed it

MySQL is a popular relational database management system that enables users to store, manage and retrieve data. It is a client/server system that uses the SQL language to access the database. However, when using MySQL, you may encounter the situation of deleting duplicate records, which is a very common problem in actual applications. Below we will introduce how to delete duplicate records in MySQL.

  1. Use the DISTINCT keyword:

DISTINCT is a keyword used to delete duplicate data in the database and can be used in SELECT statements. For example, the following code selects different data from a table:

SELECT DISTINCT column_name FROM table_name;
Copy after login

This returns a different set of records. We can use this statement to delete duplicate records in the table. First, we need to create a new table to hold data without duplicate records. Here is the code snippet of how to remove duplicate records using DISTINCT keyword:

CREATE TABLE new_table AS SELECT DISTINCT * FROM old_table;
Copy after login

Using this statement, we can create a new table and insert different records into the new table.

  1. Using GROUP BY:

The GROUP BY statement is used to group the result set by one or more columns, and optionally apply an aggregate function to each group . For example, the following code can select the sum of each column in a table:

SELECT column_name, SUM(column_name) FROM table_name GROUP BY column_name;
Copy after login

More complex SELECT statements can use multiple columns, and optionally use multiple aggregate functions in each group. The GROUP BY statement can also be used to remove duplicate data. Here is the code snippet of how to delete duplicate records using GROUP BY in MySQL:

DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP BY column_name);
Copy after login

Using this statement, we can select a table and delete all duplicate records.

  1. Use UNIQUE index:

UNIQUE index is a constraint that requires each value to appear only once in the table. This is accomplished by creating an index on the table. MySQL throws an error when trying to insert rows with the same unique index value. We can use this feature to remove duplicate records. Here is the code snippet of how to remove duplicate records using UNIQUE index in MySQL:

ALTER IGNORE TABLE table_name ADD UNIQUE (column_name);
Copy after login

Using this statement, we can create a unique index and ignore duplicate records when trying to insert them.

Summary

In MySQL, deleting duplicate records is a very common task. Although there are several different ways to accomplish this task, using DISTINCT, GROUP BY, and UNIQUE indexes is one of the most common. Whichever method you choose, you should carefully test and back up your database to ensure you don't accidentally delete important records or data.

The above is the detailed content of How to delete duplicate records in mysql. 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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!