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 | [email protected] |
+---------------------+---------------------+
| 3 | [email protected] |
+---------------------+---------------------+
| 4 | [email protected] |
+---------------------+---------------------+
| 5 | [email protected] |
+---------------------+---------------------+
I hope it could help you...
- How to Increase max_connections in MySQL?
- How to Install the PHP PDO MySQL Extension on Ubuntu 22.04?
- How to Install PHP MySQL on Linux Ubuntu?
- How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu?
- How to Change MySQL Root User Password Ubuntu?
- PHP 8 MySQL Form Validation Example
- How to Select First Row Only in PHP MySQL?
- How To Backup MySQL Database Using PHP Example