OMG I'm going crazy. Multiple Join Help!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Pryme
Forum Newbie
Posts: 2
Joined: Thu Jun 10, 2010 3:11 pm

OMG I'm going crazy. Multiple Join Help!

Post by Pryme »

I've been trying to figure this out for days. Can someone help me???

Ok, so a user logs in creating a $_SESSION['userName']. This user has the ability to sign up for an event from a list of events. The problem I'm having is I need the user to only be presented with the events he HASN'T signed up for yet.

3 Tables: Users(Primary Key=userName), Events(Primary Key=eventID), and UsersEvents(Keys=userName and eventID).

So I need to come up with a sql statement along the lines of "SELECT eventIDs that's not associated with $_SESSION['userName'] in the UsersEvents table"

This is what I've tried:

"SELECT Events.eventID, UsersEvents.eventID FROM Events, UsersEvents WHERE UsersEvents.eventID != Events.eventID AND UsersEvents.userName != '".$_SESSION['userName']."'"

It's does not work. I'm not very experienced with joins.

Any help would be freakin awesome.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: OMG I'm going crazy. Multiple Join Help!

Post by Eran »

Negative left join -

Code: Select all

$query = "SELECT Events.eventID FROM Events
LEFT JOIN UsersEvents ON UsersEvents.eventID = Events.eventID AND UsersEvents.userName = '".$_SESSION['userName'] . "' 
WHERE UsersEvents.eventID IS NULL";
Pryme
Forum Newbie
Posts: 2
Joined: Thu Jun 10, 2010 3:11 pm

Re: OMG I'm going crazy. Multiple Join Help!

Post by Pryme »

Thanks for the response! I'll have to wait till I go into work tomorrow to see if this work but I thank you anyways.
Post Reply