
- 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
Count duplicates records in MySQL table?
You can use if() from MySQL to count duplicate records. The syntax is as follows −
SELECT yourColumnName, COUNT(*) AS anyVariableName, IF ( COUNT(*)>1,"Duplicate Records", "Not Duplicate records") as anyVariableName FROM yourTableName group by yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table DuplicateRecords -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(30), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into DuplicateRecords(Name) values('Carol'); Query OK, 1 row affected (0.81 sec) mysql> insert into DuplicateRecords(Name) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DuplicateRecords(Name) values('Sam'); Query OK, 1 row affected (0.19 sec) mysql> insert into DuplicateRecords(Name) values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DuplicateRecords(Name) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DuplicateRecords(Name) values('Sam'); Query OK, 1 row affected (0.20 sec) mysql> insert into DuplicateRecords(Name) values('John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DuplicateRecords(Name) values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DuplicateRecords(Name) values('Carol'); Query OK, 1 row affected (0.10 sec) mysql> insert into DuplicateRecords(Name) values('Mike'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from DuplicateRecords;
The following is the output −
+----+-------+ | Id | Name | +----+-------+ | 1 | Carol | | 2 | John | | 3 | Sam | | 4 | John | | 5 | Sam | | 6 | Sam | | 7 | John | | 8 | Carol | | 9 | Carol | | 10 | Mike | +----+-------+ 10 rows in set (0.00 sec)
Here is the query to count the duplicate records from the table −
mysql> SELECT Name, COUNT(*) AS Repetition, IF (COUNT(*)>1,"Duplicate Records", "Not Duplicate records") as IsDuplicateRecordsOrNot -> from DuplicateRecords group by Name;
The following is the output −
+-------+------------+-------------------------+ | Name | Repetition | IsDuplicateRecordsOrNot | +-------+------------+-------------------------+ | Carol | 3 | Duplicate Records | | John | 3 | Duplicate Records | | Sam | 3 | Duplicate Records | | Mike | 1 | Not Duplicate records | +-------+------------+-------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to alphabetize records and count the duplicates?
- Insert non-duplicates value in a MySQL table
- How do we count the total duplicate records in a column of MySQL table?
- How do we count the records from MySQL table where column holds duplicate/triplicates data?
- Delete all records from a table in MySQL?
- How to implement Count (*) as variable from MySQL to display the number of records in a table?
- How to check for duplicates in MySQL table over multiple columns?
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Use TRIM on all records in a MySQL table?
- Display table records from a stored procedure in MySQL
- Delete records from a MySQL table by excluding non-deleted records using NOT IN
- Delete all the records from a MySQL table?
- How to delete duplicates and leave one row in a table in MySQL?
- Display selected records from a MySQL table with IN() operator
- Inserting records into a MySQL table using PreparedStatement in Java?

Advertisements