比赛入场限制为每天一次

发布于 2024-10-25 11:04:42 字数 462 浏览 2 评论 0原文

我正在为一个会员网站编写一个 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 小时一次 - 如果是后者,请为我指明正确的方向。

TIA:)

I'm writing a PHP competition script for a members site that needs to restrict entries to one per day per member. So far I have the following MySQL code:

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 is the competition ID,
  • ce_sub_id is the member ID, and
  • cte_date is a MYSQL datetime stamp for the entry.

It's hard for me to test from where I am now & I need to find a solution, so I'm hoping someone can tell me whether this is restricting to once-per-day or once-per-24hrs - and point me in the right direction if it's the latter.

TIA :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-11-01 11:06:11

未经测试无法确定,但我大胆猜测每 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())

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

Can't be sure without testing, but I'd hazard a guess that it's once every 24h.

Try the following instead:

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

Here you are clearly comparing two dates, one from your field and the other from the current date for equality.

一江春梦 2024-11-01 11:05:55

我希望有人能告诉我这是否限制为每天一次或每 24 小时一次

看起来是 24 小时:

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)

如果您需要“明天”,例如今晚 23:59 过一分钟,请考虑按照简单的DATE 的解决方案进行咀嚼:

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)

通过仅考虑日期而不是时间,您的截止时间实际上将在午夜到期。但请注意,您将受到 MySQL 服务器上的时间的影响,如果应用程序服务器位于不同的计算机上,则该时间可能与应用程序服务器上的时间不同。

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.

旧情别恋 2024-11-01 11:05:37

创建由 user_id、competition_id 和日期类型列组成的主键。

检查用户是否已经输入:

select count(*)
from competition_entries
where ce_c_id = '$c_id' 
    AND ce_sub_id = '$user_id' 
    AND cte_date = current_date()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文