
- 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 duplicates and leave one row in a table in MySQL?
To delete duplicates and leave one row in a table, you need to use the concept of temporary table. The steps are as follows −
create table anytemporaryTableName as select distinct yourColumnName1, yourColumnName2 from yourTableName; truncate table yourTableName; insert into yourTableName(yourColumnName1, yourColumnName2) select yourColumnName1, yourColumnName2 from yourtemporaryTableName ; drop table yourtemporaryTableName ;
Let us create a table −
mysql> create table demo39 −> ( −> user_id int, −> user_name varchar(20) −> ); Query OK, 0 rows affected (0.74 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo39 values(10,'John'); Query OK, 1 row affected (0.19 sec) mysql> insert into demo39 values(10,'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo39 values(11,'David'); Query OK, 1 row affected (0.20 sec) mysql> insert into demo39 values(11,'David'); Query OK, 1 row affected (0.17 sec)
Display records from the table using select statement −
mysql> select *from demo39;
This will produce the following output −
+---------+-----------+ | user_id | user_name | +---------+-----------+ | 10 | John | | 10 | John | | 11 | David | | 11 | David | +---------+-----------+ 4 rows in set (0.00 sec)
Following is the query to delete duplicates and leave one row in a table −
mysql> create table temporaryTable as select distinct user_id, user_name from demo39; Query OK, 2 rows affected (1.39 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> truncate table demo39; Query OK, 0 rows affected (2.30 sec) mysql> insert into demo39(user_id, user_name) select user_id, user_name from temporaryTable; Query OK, 2 rows affected (0.16 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> drop table temporaryTable; Query OK, 0 rows affected (1.01 sec)
Display records from the table using select statement −
mysql> select *from demo39;
This will produce the following output −
+---------+-----------+ | user_id | user_name | +---------+-----------+ | 10 | John | | 11 | David | +---------+-----------+ 2 rows in set (0.00 sec)
- Related Articles
- How to Add Edit and Delete Table Row in jQuery?
- How can we delete a single row from a MySQL table?
- Does deleting row from view delete row from base table in MySQL?
- How to delete a row from a table using jQuery?
- Delete one row and reorder the others with the correct ID in MySQL?
- How to delete a column from a table in MySQL?
- MySQL query to delete row
- Count duplicates records in MySQL table?
- Insert non-duplicates value in a MySQL table
- Delete more than one rows from a table using id in MySQL?
- What happens if I will delete a row from MySQL parent table?
- How to check for duplicates in MySQL table over multiple columns?
- How to exclude a specific row from a table in MySQL?
- How to delete all the duplicate records in a MySQL table?
- How to find and remove duplicates from a table in Oracle?

Advertisements