Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find duplicate email within a mysql table

I want to fetch duplicate email from table:

userid      email
-------------------------
1       [email protected]
2       [email protected]
3       abc%40gmail.com
4       [email protected]
5       abcd%40gmail.com

So from above records i want result like

Email          Count
-------------------------
[email protected]   2
[email protected]  2
[email protected]   1

Does anybody know how to manage that?

Thanks.

like image 435
Steve Martin Avatar asked Aug 15 '13 06:08

Steve Martin


People also ask

How do I find duplicate emails in a table?

Finding duplicate emails By using Sub-query with EXISTS: In a correlated subquery, the inner query is executed for each record in the outer query. So one email is compared to the rest of the email in the same table using a correlated subquery and EXISTS clause in SQL as shown below.

How do I find duplicates in a DB table?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

How do I find duplicate names in a table in SQL?

To find the duplicate Names in the table, we have to follow these steps: Defining the criteria: At first, you need to define the criteria for finding the duplicate Names. You might want to search in a single column or more than that. Write the query: Then simply write the query to find the duplicate Names.


3 Answers

If you want to output the data exactly like shown in your question, use this query:

SELECT email, COUNT(*) AS count
FROM table
GROUP BY email HAVING count > 0
ORDER BY count DESC;
like image 161
derhansen Avatar answered Oct 04 '22 22:10

derhansen


You can't directly do that in MySQL because there is no function to urlencode or urldecode strings.

You will have to create a User Defined Function to handle that process. Once you have that function just go for a simple group by with a having clause.

Link to the required UDFs

If UDFs are not an option, the only workaround I can think of is manually replacing the chars (under your own risk):

SELECT REPLACE(email, "%40", "@") DuplicateEmail, COUNT(*) Amount
FROM t
GROUP BY DuplicateEmail
ORDER BY Amount desc

Fiddle here.

Output:

| DUPLICATEEMAIL | AMOUNT |
---------------------------
|  [email protected] |      2 |
| [email protected] |      2 |
|  [email protected] |      1 |
like image 25
Mosty Mostacho Avatar answered Oct 04 '22 21:10

Mosty Mostacho


Here is a simple solution:

SELECT email, COUNT(1) FROM table_name GROUP BY email HAVING COUNT(1) > 1
like image 45
Swapnil Patil Avatar answered Oct 04 '22 20:10

Swapnil Patil