0

Context: event scheduling software.

Thanks to answers on this question about finding duplicate values, I have come up with the following query, which works perfectly for finding users who are scheduled for two events at the same time:

SELECT  *
FROM    panel_participants pp
WHERE   EXISTS
    (
    SELECT  1
    FROM    panel_participants pps
    WHERE   pp.user_id = pps.user_id
    AND     pp.day_time_slot_id = pps.day_time_slot_id
    AND     pp.day_time_slot_id > 0
    LIMIT 1, 1
    )

Now what I need is a very similar query, with one difference: I want to find items where pp.day_time_slot_id = pps.day_time_slot_id + 2 (which will show me where a user has been scheduled for two consecutive events). However, this returns zero rows:

SELECT  *
FROM    panel_participants pp
WHERE   EXISTS
    (
    SELECT  1
    FROM    panel_participants pps
    WHERE   pp.user_id = pps.user_id
    AND     pp.day_time_slot_id = pps.day_time_slot_id + 2
    AND     pp.day_time_slot_id > 0
    LIMIT 1, 1
    )

And I know there are rows that should qualify, e.g. user 503 is scheduled for items in time slots 23 and 25.

I've tried setting variables based on the replies to this question, but I think I'm not understanding how variables work and are passed into and out of queries, because everything I try returns either an error or zero rows. I've confirmed that day_time_slot_id is an integer in case that makes a difference.

Help?

4

1 回答 1

1

假设您在 panel_participants 表上有一个 panel_id,以下应该会为您提供在同一时间段内有 2 个面板参与的人员列表:-

SELECT  DISTINCT pp.user_id
FROM    panel_participants pp
INNER JOIN panel_participants pps
ON pp.user_id = pps.user_id
AND pp.day_time_slot_id = pps.day_time_slot_id
AND pp.panel_id != pps.panel_id
AND pp.day_time_slot_id > 0

下面应该找到那些预约了 2 个连续插槽的人(尽管不确定为什么连续插槽应该分开 2 个)。

SELECT  DISTINCT pp.user_id
FROM    panel_participants pp
INNER JOIN panel_participants pps
ON pp.user_id = pps.user_id
AND pp.day_time_slot_id + 2 = pps.day_time_slot_id
AND pp.day_time_slot_id > 0

恐怕我无法测试(或确定),因为您没有发布任何表格布局或数据。

于 2013-06-20T08:30:57.897 回答