Skip to content

All topics  /  MySQL

How to Check Duplicate Records in PHP MySQL Example?

MySQL ·

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                   | [email protected]      |
+---------------------+---------------------+
| 3                   | [email protected]      |
+---------------------+---------------------+
| 4                   | [email protected]       |
+---------------------+---------------------+ 
| 5                   | [email protected]       |
+---------------------+---------------------+

I hope it could help you...