开发者

Does this MySQL query always return the expected result?

I wrote a query as follows:

  SELECT COUNT(*) AS count, email
    FROM sometable
GROUP BY email
ORDER BY count DESC 
   LIMIT 4

I am interested in seeing the four most duplicated email entries in the table. So far, it seems to return exactly what I want:

count   email
12      very-duplicated@email.com
2       duped-twice@email.com
2       also-twice@email.com
1       single@email.com

When I don't use LIMIT, I get the same result (albeit with many more rows having a count = 1). What I'm wondering about is the LIMIT. In the future, when the numbers change, will my query above still return the four most used emails? or 开发者_JAVA百科does the query need to scan the entire database to remain accurate?

(note: I am not trying to prevent duplicates, I'm trying to see the most frequently used email.)


I'm not sure. But if you're concerned, you could apply a limit to a subquery:

select *
from 
(
  SELECT COUNT(*) AS count, email
  FROM sometable
  GROUP BY email
  ORDER BY count DESC 
)
limit 4

Alternateively, you could do something like this to see all duplicated email address (may return more or less than 4):

  SELECT COUNT(*) AS count, email
    FROM sometable
GROUP BY email
having COUNT(email) > 1
ORDER BY count DESC 


Well first thing is, the query does not only return you the duplicate entries. Look at 4th row which says count = 1 which means it occurs only once in the table. To list duplicate records you need to modify your query as -

SELECT COUNT(*) AS count, email
FROM sometable
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY count DESC     
LIMIT 4

Then, this will always return you 4 topmost duplicate entries in your table as the order mentioned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜