gpt4 book ai didi

php - 选择用户参与的所有即将发生的事件

转载 作者:行者123 更新时间:2023-11-29 13:07:53 25 4
gpt4 key购买 nike

我的 SQL 有问题,该 SQL 应该选择特定用户参与的所有即将发生的事件。这是我的表的示例

事件

  id  |      timestamp      |  name  | deleted
200 | 2014-04-01 14:00:00 | test 1 | 0
201 | 2013-06-30 20:50:00 | test 2 | 0
205 | 2014-04-02 09:00:00 | test 3 | 1
210 | 2014-04-03 08:55:34 | test 4 | 0

回应

  id  | user | event |      timestamp      | canceled
1001 | 50 | 201 | 2013-06-01 10:15:39 | 0
1002 | 23 | 205 | 2014-02-15 09:32:00 | 0
1003 | 50 | 210 | 2014-02-15 10:00:00 | 0
1004 | 50 | 210 | 2014-02-16 20:00:00 | 1
1005 | 50 | 200 | 2014-02-16 20:05:40 | 0

解释一下结构:如果管理员删除了某个事件,则该事件的已删除值将设置为 1。如果用户注册了某个事件,则 responses 中会添加该用户的新行id、事件id、当前时间和cancels = 0。他也可以取消他的注册。在这种情况下,将在 responses 中添加一个新行,并取消 = 1。因此,每次用户对事件的注册状态发生更改时,都会添加一个新行。用户有可能注册了某个事件,然后取消了注册,然后再次注册了同一事件。

现在我想选择 id 为 50 的用户已注册(且尚未取消注册)的所有即将发生的事件。这应该是结果:

event.id |   event.timestamp   | responses.id
200 | 2014-04-01 14:00:00 | 1005

目前我正在使用 SELECT 语句和 php 来获取上面显示的结果。编辑这就是我现在所做的:首先我这样做

"SELECT id,timestamp FROM events WHERE timestamp>NOW() AND deleted=0"

然后我运行一个 php foreach 循环来为每个即将发生的事件执行此语句

"SELECT id,canceled FROM responses WHERE user = 50 AND event=$event->id ORDER BY timestamp DESC LIMIT 1"

如果一行存在,我用 php 检查取消是否为 0。

如何用单个 SELECT 语句解决问题?

最佳答案

您可以子选择MAX(canceled)以查看用户是否取消了注册。类似于:

SELECT resp.user, events.id, events.name, events.timestamp AS event_time
FROM events
LEFT JOIN (SELECT user, event, MAX(canceled) canceled FROM responses GROUP BY user, event) resp
ON (events.id=resp.event)
WHERE events.timestamp>NOW() AND events.deleted=0 AND resp.canceled=0

关于php - 选择用户参与的所有即将发生的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22465653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com