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?