所以我有一个图像上传器,我想创建一个最小的机器人保护。我的表结构如下所示:
| ID | uploader_ip | image_name | image_url_id | date
------------------------------------------------------
| 1 | 127.0.0.1 | bla.jpg | fsdJGf | UNIX Timestamp (UPDATE ON: Create)
我想检查是否有更多或正好 50 张图像/行,这些图像/行是在不到或正好两分钟内创建的。
我该怎么做?
您需要
构造一个包含计数和组的查询,并在日期列上放置一个范围。 如果仅在最近两分钟内执行此操作,则查询将如下所示:
select count(*) from your_table where uploader_ip="<some ip>" and date > (now() - interval 2 minute) group by uploader_ip;
这是查询
Select count(*) from table
where
table.date > now() - interval 2 minute
-- or the function date_sub(now(), interval 2 minute)
and id = 1
如果您希望所有用户 ID 在 2 分钟内上传:
select id,
(select count(*)
from table
where id = t.id
and date > t.date - interval 1 minute
and date < t.date + interval 1 minute
) as total
from table t
where total >= 50