Filter data in select to omit records related to logged in user.
I wasn't sure where to post this. I can add more details but figured I would start with the basics in hopes it was just something I wasn't grasping but might be an easy fix.
I have a page that people will log in to and then see a list of event dates. They will also see a form with 2 selects. A drop down select with the event date and a second with a number of tables to choose. This form inserts a record into a relational table. All of this is working.
Everything working but the date drop down. See, it's still showing a date that is already in the user's reserved list.
Here are two of my tables. (there's a third for users info.)
Parent event table
Child user to event table
Here's what I need. Since the dropdown is dynamically populated by a recordset, it needs to show only dates that the user has not already registered for. Basically the opposite of the other list that displays the events they DID register for. I need this list (drop down in form) to show the events they did not register for.
My attempts so far have given me partial results.
Here is my SQL for the list that shows their registrations.
SELECT users.UserID, date_format(show_date, '%M %e, %Y') as show_date, UserFirstName, UserLastName, reserved
FROM tblbooths LEFT JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
LEFT JOIN users ON users.UserID = tblreserved.UserID
WHERE tblreserved.UserID = ParamUserID
GROUP BY show_date
(with)
ParamUserID
integer
-1
$_SESSION['UserID']
How would I create the opposite that would show all event dates that the user has not already registered for? Seems simple I know. But using WHERE UserID != ParamUserID won't do it because there are records that do not have the user's id in it, but still have the date they already registered for. So I tried HAVING so it would do it after the grouping of dates. That partially works, but still leaves a couple dates in there for some reason.
SQL for the select that I have tried, but doesn't work.
SELECT capacity, date_format(show_date, '%M %e, %Y') as show_date, tblbooths.booth_id, COALESCE(sum(reserved), 0) AS total, UserID, capacity - COALESCE(sum(reserved), 0) AS available
FROM tblbooths LEFT JOIN tblreserved ON tblbooths.booth_id = tblreserved.booth_id
WHERE show_date >= CURDATE()
GROUP BY show_date
HAVING UserID != ParamUserID
Ideally I would like just one recordset that worked for both areas. But that's not a big deal. On that note. Is there a way to bind the first recordset to both areas in php and maybe an if statement? I would alter it a little. Just a thought.
Thanks,
TroyD