mysql计数重复

时间:2011-02-25 16:49:16

标签: mysql sql

我有一张这样的表

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)

我需要创建一个查询,通过电子邮件计算所有副本,如

 | 20061129180346 | test@test.com | 1^31^9 
 | 20061129330638 | test@test.com | 1^31^9

5 个答案:

答案 0 :(得分:10)

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

最后一个(HAVING)子句将选择限制为> 1的电子邮件计数,例如重复。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

我不明白你的第二个阻止,但要计算每封电子邮件

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

这将计算表格中每个电子邮件地址的数量,而不考虑电子邮件地址的情况。例如,“test@test.com”和“TEST@TEST.COM”将被视为相同的电子邮件地址。它也只显示重复项,任何一次发生的电子邮件地址都不会被返回。如果你想要返回这些,只需省略“HAVING”子句。

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