I need to count the number of duplicate emails in a mysql database, but without counting the first one (considered the original). In this table, the query result should be the single value “3” (2 duplicate x@q.com plus 1 duplicate f@q.com).
TABLE
ID | Name | Email
1 | Mike | x@q.com
2 | Peter | p@q.com
3 | Mike | x@q.com
4 | Mike | x@q.com
5 | Frank | f@q.com
6 | Jim | f@q.com
My current query produces not one number, but multiple rows, one per email address regardless of how many duplicates of this email are in the table:
SELECT value, count(lds1.leadid) FROM leads_form_element lds1 LEFT JOIN leads lds2 ON lds1.leadID = lds2.leadID
WHERE lds2.typesID = "31" AND lds1.formElementID = '97'
GROUP BY lds1.value HAVING ( COUNT(lds1.value) > 1 )
It’s not one query so I’m not sure if it would work in your case, but you could do one query to select the total number of rows, a second query to select distinct email addresses, and subtract the two. This would give you the total number of duplicates…
In fact, I don’t know if this will work, but you could try doing it all in one query:
Like I said, untested, but let me know if it works for you.