2

我正在为一个会员网站编写一个 PHP 竞赛脚本,该网站需要将每个会员每天的条目限制为一个。到目前为止,我有以下 MySQL 代码:

SELECT ce_id 
  FROM competition_entries 
 WHERE ce_c_id = '$c_id' 
       AND ce_sub_id = '$user_id' 
       AND cte_date >= SYSDATE() - INTERVAL 1 DAY
  • ce_c_id 是比赛ID,
  • ce_sub_id 是成员 ID,并且
  • cte_date 是条目的 MYSQL 日期时间戳。

我很难从现在的位置进行测试,我需要找到解决方案,所以我希望有人能告诉我这是否限制为每天一次或每 24 小时一次 - 并指出我如果是后者,方向正确。

蒂亚:)

4

3 回答 3

2

Create a primary key composed of the user_id, competition_id and a date type column.

To check if the user has already placed an entry:

select count(*)
from competition_entries
where ce_c_id = '$c_id' 
    AND ce_sub_id = '$user_id' 
    AND cte_date = current_date()
于 2011-03-21T22:52:04.040 回答
1

I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs

Looks like it's 24 hours:

mysql> select sysdate(), sysdate() + interval 1 day;
+---------------------+----------------------------+
| sysdate()           | sysdate() + interval 1 day |
+---------------------+----------------------------+
| 2011-03-21 15:50:56 | 2011-03-22 15:50:56        | 
+---------------------+----------------------------+
1 row in set (0.01 sec)

If you need "tomorrow", as in, tonight at one minute past 23:59, consider chomping down things to plain old DATE's resolution:

mysql> select DATE(sysdate()), DATE(sysdate()) + interval 1 day;
+-----------------+----------------------------------+
| DATE(sysdate()) | DATE(sysdate()) + interval 1 day |
+-----------------+----------------------------------+
| 2011-03-21      | 2011-03-22                       | 
+-----------------+----------------------------------+
1 row in set (0.00 sec)

By just considering dates instead of times, your cutoff will effectively expire at midnight. Watch out, though -- you'll then be at the mercy of the time on your MySQL server, which might differ from the time on your application server if they are on different machines.

于 2011-03-21T22:51:59.983 回答
1

如果没有测试就无法确定,但我敢猜测它是每 24 小时一次。

请尝试以下操作:

SELECT ce_id FROM competition_entries WHERE ce_c_id = '$c_id' AND ce_sub_id = '$user_id' AND DATE(cte_date) == DATE(SYSDATE())

在这里,您清楚地比较了两个日期,一个来自您的领域,另一个来自当前日期是否相等。

于 2011-03-21T22:56:32.920 回答