如何查询在5分钟内添加超过2次且具有相同电子邮件地址的重复数据?

时间:2016-01-29 15:28:01

标签: php mysql sql yii2

我在数据库表中有我的样本数据如下。

id   email            created_at
1    e@mail.com       2016-01-01 01:01:30
2    e@mail.com       2016-01-01 01:02:20
3    e@mail.com       2016-01-01 01:03:30
4    new@mail.com     2016-01-01 02:56:00
5    e@mail.com       2016-01-01 01:04:30
6    new@mail.com     2016-01-01 02:59:08
7    new@mail.com     2016-01-01 03:01:24
8    i@mail.com       2016-01-01 04:20:30
9    i@mail.com       2016-01-01 04:23:29
10   new@mail.com     2016-01-01 04:30:08
11   i@mail.com       2016-01-01 04:25:29
12   new@mail.com     2016-01-01 04:32:08
13   e@mail.com       2016-01-01 05:16:30
14   i@mail.com       2016-01-01 06:00:00
15   aa@email.com     2017-07-17 15:03:00
16   aa@email.com     2017-07-17 15:04:00
17   aa@email.com     2017-07-17 15:08:01

我运行以下SQL查询,由Strawberry提供。

SELECT x.* 
FROM my_table x
JOIN my_table y
ON y.id <> x.id
AND y.email = x.email
AND y.created_at 
BETWEEN x.created_at - INTERVAL 5 MINUTE 
AND x.created_at + INTERVAL 5 MINUTE 
GROUP
BY x.id HAVING COUNT(*) >= 2

我得到以下记录。

id   email            created_at
1    e@mail.com       2016-01-01 01:01:30
2    e@mail.com       2016-01-01 01:02:20
3    e@mail.com       2016-01-01 01:03:30
5    e@mail.com       2016-01-01 01:04:30
6    new@mail.com     2016-01-01 02:59:08
8    i@mail.com       2016-01-01 04:20:30
9    i@mail.com       2016-01-01 04:23:29
11   i@mail.com       2016-01-01 04:25:29
16   aa@email.com     2017-07-17 15:04:00

我想要检索的是以下记录,因为它们具有相同的电子邮件地址。它对我来说意味着相同的记录,并且它们在5分钟内插入了2次以上。

id   email            created_at
1    e@mail.com       2016-01-01 01:01:30
2    e@mail.com       2016-01-01 01:02:20
3    e@mail.com       2016-01-01 01:03:30
5    e@mail.com       2016-01-01 01:04:30
8    i@mail.com       2016-01-01 04:20:30
9    i@mail.com       2016-01-01 04:23:29
11   i@mail.com       2016-01-01 04:25:29

如何编写sql查询以仅获取在5分钟内添加超过2次的记录?

3 个答案:

答案 0 :(得分:2)

尝试:

Select T1.*
from emails T1
inner join emails T2 on
T1.email=T2.email and T2.id=T1.id+1 and T2.created_at<T1.created_at + Interval 5 minute
union 
Select T2.*
from emails T1
inner join emails T2 on
T1.email=T2.email and T2.id=T1.id+1 and T2.created_at<T1.created_at + Interval 5 minute
order by ID

小提琴:http://sqlfiddle.com/#!9/b16171/12

答案 1 :(得分:2)

可能有一种更简单的方法,但这似乎会返回您正在寻找的结果......

SELECT DISTINCT a.*
           FROM service_request a
           JOIN 
              ( SELECT x.* 
                     , MAX(y.created_at) AS range_end
                  FROM service_request x
                  JOIN service_request y
                    ON y.email = x.email
                   AND y.id >= x.id 
                   AND y.created_at <= x.created_at + INTERVAL 5 MINUTE
                 GROUP
                    BY x.id HAVING COUNT(*) >= 3
              ) b
             ON b.email = a.email 
            AND a.created_at BETWEEN b.created_at AND b.range_end;

答案 2 :(得分:2)

either you need to remove ID = 4 or to add Id = 11 and ID = 12 and check this:


select  distinct
        id,
        Email,
        Created_at
from    (
            select  distinct
                    t1.id,
                    t1.email,
                    t1.created_at
            from    MyTable t1
                    join MyTable t2 on
                        t2.email = t1.email
            where   datediff(mi, t1.created_at, t2.created_at) > 0
                    and datediff(mi, t1.created_at, t2.created_at) <= 5
            union all
            select  distinct
                    t2.id,
                    t2.email,
                    t2.created_at
            from    MyTable t1
                    join MyTable t2 on
                        t2.email = t1.email
            where   datediff(mi, t1.created_at, t2.created_at) > 0
                    and datediff(mi, t1.created_at, t2.created_at) <= 5
        ) x