How to Check Duplicate Records in PHP MySQL Example?

03-Apr-2023

.

Admin

How to Check Duplicate Records in PHP MySQL Example?

Hi Dev,

This article goes in detailed on how to check duplicate records in php mysql example. we will help you to give example of how to find duplicate records in php mysql. I would like to show you MySQL Find Duplicate Records (Rows). In this article, we will implement a mysql find duplicate values multiple columns.

Let’s take an example, let’s have one table name users and where we will check duplicate rows using the email column in the users table. So we can use the below MySQL query to find duplicate rows in your database table with the count.

Example 1:Find Duplicate Rows


SELECT

id,

COUNT(email)

FROM

users

GROUP BY email

HAVING COUNT(email) > 1;

Output:

+---------------------+---------------------+

| id | Count |

+---------------------+---------------------+

| 2 | 5 |

+---------------------+---------------------+

| 4 | 3 |

+---------------------+---------------------+

Example 2:Find Duplicate Records

SELECT id, email

FROM users

WHERE email IN (

SELECT email

FROM users

GROUP BY email

HAVING count(email) > 1

)

ORDER BY email

Output:

+---------------------+---------------------+

| id | Email |

+---------------------+---------------------+

| 2 | test@gmail.com |

+---------------------+---------------------+

| 3 | test@gmail.com |

+---------------------+---------------------+

| 4 | way@gmail.com |

+---------------------+---------------------+

| 5 | way@gmail.com |

+---------------------+---------------------+

I hope it could help you...

#MySQL

#PHP