0

我有一个基本上为特定用户收集选票的表格:

ID | user_id | ip | timestamp

每次有人为用户投票时,都会创建一个条目,记录他们投票的 user_id、他们的 IP 地址以及时间和日期。为了防止作弊,我们不允许每个用户每个 IP 每天超过 20 票。

现在我想从给定日期开始验证这些投票,我自己提出了以下查询,但我绝不是专家,所以如果有人能告诉我我是否正确,我将不胜感激它。

SELECT count(*) As `votes`, user_id, ip, DATE( timestamp ) the_date
FROM `user_votes`
GROUP BY user_id, ip, the_date
HAVING the_date > '2012-07-26'
ORDER BY the_date ASC, user_id ASC, votes DESC
4

2 回答 2

0

尝试这个:

SELECT count(*) As `votes`, user_id, ip, DATE( timestamp ) the_date
FROM `user_votes`
where the_date > '2012-07-26'
GROUP BY user_id, ip, DATE( timestamp )
having count(*)>20 
ORDER BY DATE( timestamp ) ASC, user_id ASC, votes DESC

count(*)>20 会让你的计数超过 20

于 2012-08-02T07:09:56.520 回答
0

要获得有效票数的用户,您可以尝试以下操作:

SELECT COUNT(*) As `votes`, user_id, ip, DATE( timestamp ) the_date
FROM `user_votes`
WHERE DATE(timestamp ) > '2012-07-26'
GROUP BY user_id, ip, the_date
HAVING COUNT(*) <= 20
ORDER BY the_date ASC, user_id ASC, votes DESC;
于 2012-08-02T07:10:25.850 回答