简体   繁体   English

在MySQL中获取不同的数据

[英]Getting distinct data in MySQL

At the moment I'm on the process of getting the counts of the unique email address in the database . 目前,我正在获取数据库中唯一电子邮件地址的计数。

Table1
---------------------------------------------
 id    email          name      date_applied
---------------------------------------------
 1    abc@mail.com   ABC       2013-04-17
 2    bcd@mail.com   BCD       2013-04-18
 3    xyz@mail.com   XYZ       2013-04-20
 4    tre@mail.com   TRE       2013-04-28
 5    esf@mail.com   ESF       2013-04-29
 6    bcd@mail.com   BCD       2013-04-29
 7    abc@mail.com   ABC       2013-04-30
---------------------------------------------

Here's the query on how I get the unique emails for the week: 这是关于我本周如何获得唯一电子邮件的查询:

select count(distinct(`A`.`email`)) as total 
from Table1 as A 
where A.date_applied BETWEEN `2013-04-29` and `2013-05-05`

Current Week: 2013-04-29 => 2013-05-05

What I wanted to achieve is to get the count of the new emails in the current week without ignoring the emails in the previous weeks. 我想要实现的是在不忽略前几周的电子邮件的情况下,获取当前周中新电子邮件的计数。

I badly needed your help. 我急需您的帮助。

You can achieve this by using sub-query like this: 您可以通过使用如下子查询来实现:

SELECT COUNT(DISTINCT `email`) AS Total FROM Table1 AS A 
  WHERE A.date_applied 
BETWEEN '2013-04-29' AND '2013-05-05'
    AND A.email NOT IN (SELECT email FROM Table1
                         WHERE date_applied < '2013-04-29');

Result: 结果:

| TOTAL |
---------
|     1 |

See this SQLFiddle 看到这个SQLFiddle


Here is the description of the query: 这是查询的描述:

First of all you need to get email between two dates (ie a week) like this: 首先,您需要在两个日期(即一周)之间获取电子邮件,如下所示:

SELECT DISTINCT email FROM Table1
 WHERE date_applied BETWEEN '2013-04-29' AND '2013-05-05'

Then search for emails which are not unique (ie already exists on previous date): 然后搜索不是唯一的电子邮件(即在上一个日期已经存在):

SELECT email 
  FROM Table1
 WHERE date_applied < '2013-04-29'

Now exclude those emails from our current query by using NOT IN : 现在,使用NOT IN将那些电子邮件从当前查询中排除:

SELECT DISTINCT `email` FROM Table1 AS A 
  WHERE A.date_applied 
BETWEEN '2013-04-29' AND '2013-05-05'
    AND A.email NOT IN (SELECT email FROM Table1
                         WHERE date_applied < '2013-04-29');

Now use COUNT() function to get the count of emails. 现在使用COUNT()函数来获取电子邮件的数量。

See this SQLFiddle 看到这个SQLFiddle

You'll want to aggregate. 您将要汇总。 To get the date a distinct email was added: 为了获得日期,添加了不同的电子邮件:

SELECT email, MIN(date_applied) as first
FROM Table1
GROUP BY email
HAVING first BETWEEN 2013-04-29 AND 2013-05-05

The number of rows in your result is your answer. 结果中的行数就是您的答案。

Or use a sub query. 或使用子查询。 I still have to get used to MySQL supporting them. 我仍然必须习惯于MySQL支持它们。 :) :)

You should think about your schema design, though. 不过,您应该考虑一下架构设计。 I understand this is probably a minimal example, but date_applied is not really the applicationdate in your mindmap. 我了解这可能是一个最小的示例,但date_applied并不是您的思维导图中的applicationdate。 In your mind email is KEY in this table, not Id . 在您看来,此表中的email是KEY,而不是Id Why not have the dbschema congrue with your mind? 为什么不使dbschema与您的思想相融合? Then your query will be simpler, and more performant. 这样,您的查询将变得更简单,更高效。

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

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