4

I have a table like this

mysql> desc user_changes;
+----------+--------------+------+-----+---------+-------+ 
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+ 
| id       | varchar(16)  | NO   | PRI |         |       | 
| email    | varchar(255) | YES  | MUL | NULL    |       |  
| products | longtext     | YES  |     | NULL    |       | 
+----------+--------------+------+-----+---------+-------+ 
3 rows in set (0.00 sec)

And i need to create a query that will count all the duplicate by email like this

 | 20061129180346 | [email protected] | 1^31^9 
 | 20061129330638 | [email protected] | 1^31^9
1
  • 5
    Did you need help with something or do you just want others to do it for you? Commented Feb 25, 2011 at 16:50

5 Answers 5

10
SELECT    count(*), email
FROM      user_changes
GROUP BY  email
HAVING    count(*) > 1

The last (HAVING) clause limits the selection to email counts that are >1, e.g. duplicates.

Sign up to request clarification or add additional context in comments.

Comments

1
select
    email,
    count(email) as email_count
from
    user_changes
group by
    email
having
    count(email) > 1
order by
    email asc

Comments

1

I don't understand your second block, but to get a count of each email do

SELECT email, COUNT(*) AS count FROM user_changes GROUP BY email WHERE count > 1;

Comments

1
select email, count(*)
from user_changes
group by email
having count(*) > 1

Comments

1

This will count how many of each email address there is in the table, disregarding the case of the email address. For example, "[email protected]" and "[email protected]" will be treated as the same email address. It also just shows the duplicates, any email address that just occurs once will not be returned. If you want to return these as well, just omit the "HAVING" clause.

SELECT LOWER(email) EmailAddress, COUNT(*) EmailCount
  FROM user_changes
 GROUP BY LOWER(email)
HAVING COUNT(*) > 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.