简体   繁体   English

这个MySQL查询总是返回预期的结果吗?

[英]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). 当我不使用LIMIT ,我得到相同的结果(尽管有更多行具有count = 1)。 What I'm wondering about is the LIMIT . 我想知道的是LIMIT In the future, when the numbers change, will my query above still return the four most used emails? 将来,当数字发生变化时,我上面的查询是否仍然会返回最常用的四封电子邮件? or 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): 或者,您可以执行以下操作以查看所有重复的电子邮件地址(可能返回多于或少于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. 看第四行,表示count = 1,这意味着它只在表中出现一次。 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. 然后,这将始终返回您表中提到的4个最重复的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM