php - 如何查询5分钟内插入2次以上,邮箱地址相同,status=1的重复记录?

标签 php mysql sql duplicates

我的示例数据在数据库表中如下。

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

我的要求是:

a. Records are duplicated by email
b. The duplicated records are more than 2, thus 3 and upper
c. Those 3 or upper duplicated records have been inserted within 5 minutes Interval.
d. status = 1

下面是我的 sql 查询,由@Strawberry 提供。

SELECT DISTINCT a.*
       FROM my_table a
       JOIN 
          ( SELECT x.* 
                 , MAX(y.created_at) AS range_end
              FROM my_table x
              JOIN my_table 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;

上述查询返回以下记录。

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

我试着把 "WHERE status = 1" 只得到下面的记录,因为它们符合我的要求。

id   email            created_at             status
1    e@mail.com       2016-01-01 01:01:30     1
3    e@mail.com       2016-01-01 01:03:30     1
5    e@mail.com       2016-01-01 01:04:30     1

我要检索的是同一电子邮件地址重复的记录,它们在 5 分钟内被插入超过 2 次且状态为 1。如何 "WHERE status = 1" 得到我想要的结果?

最佳答案

我认为您的查询对于 MySQL 来说有点太复杂了:

select t.*
from my_table t join
     my_table t2
     on t.email = t2.email and
        t2.created_at > t.created_at and
        t2.created_at <= date_add(t.created_at, interval 5 minute) and
        t2.status = 1
where t.id = 1
group by t.id
having count(*) >= 3;

因为 id 在您的表中是唯一的,所以可以group by 该列并从表中选择其他列。事实上,MySQL 扩展的这种使用甚至与 ANSI 标准 SQL 一致。

关于php - 如何查询5分钟内插入2次以上,邮箱地址相同,status=1的重复记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35224883/

相关文章:

sql - ORA-32033 : unsupported column aliasing : Oracle 10g

php - 具有左连接并防止空结果的 sql 查询(数据表)

php - 为什么 PHP 忽略任何字符串开头的换行符

mysql - 如何从 angularjs 接收 JSON 数据到 laravel

sql - 无法在64位SQL Server上在进程中加载​​“Microsoft.ACE.OLEDB.12.0”

mysql - 如何动态选择列? mysql

java - PHP/Java 套接字通信不工作

php stdClass 到数组

php - sql数据验证中的循环问题

mysql - Bash - 在输出中获取循环的反向计数