
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to delete all the duplicate records in a MySQL table?
To delete duplicate records from a table, we can use the DELETE command. Let us now create a table.
mysql> create table DuplicateDeleteDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)
Inserting records into the table “DuplicateDeleteDemo”: Here, we have added “John” as duplicate record 3 times.
mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DuplicateDeleteDemo values(2,'Johnson'); Query OK, 1 row affected (0.13 sec) mysql> insert into DuplicateDeleteDemo values(1,'John'); Query OK, 1 row affected (0.14 sec)
To display all records, use the SELECT statement.
mysql> select *from DuplicateDeleteDemo;
The following is the output with duplicate records.
+------+---------+ | id | name | +------+---------+ | 1 | John | | 1 | John | | 2 | Johnson | | 1 | John | +------+---------+ 4 rows in set (0.00 sec)
In the above output, we have 4 records in the table, out of which 3 records are duplicate.
To delete duplicate records, ude DELETE.
mysql> delete from DuplicateDeleteDemo where id=1; Query OK, 3 rows affected (0.19 sec)
To check if records have been deleted, let us again display all the records.
mysql> select *from DuplicateDeleteDemo;
The following output displays that all the duplicate records have been deleted.
+------+---------+ | id | name | +------+---------+ | 2 | Johnson | +------+---------+ 1 row in set (0.00 sec)
- Related Articles
- Delete all the records from a MySQL table?
- Delete all records from a table in MySQL?
- How to delete a single value from a MySQL table with duplicate records?
- How to delete all records from a table in Oracle using JDBC API?
- How to delete the duplicate values stored in reverse order from MySQL table?
- How do we count the total duplicate records in a column of MySQL table?
- Display an error while inserting duplicate records in a MySQL table
- How to convert all the records in a MySQL table from uppercase to lowercase?
- Delete records from a MySQL table by excluding non-deleted records using NOT IN
- Delete records from a MySQL table with IN() in a single query
- How can we delete all rows from a MySQL table?
- Use TRIM on all records in a MySQL table?
- How can we fetch all the records from a particular MySQL table?
- How to remove Duplicate Records except a single record in MySQL?
- Add a single year to all the date records in a MySQL table column

Advertisements