I’m working on a project where I need to do a table join with one of two columns depending on a given row’s contents. Basically, given a specific Username, I’d like to obtain information about all of their opponents in all the games they’re playing. The two tables I’m using here are GameSession and Users. GameSession has a column for each of the players(Player1 and Player2), which are foreign keys in the Users table column Username, which also contains a Rank column. Here’s my select statment:
SELECT CASE WHEN GameSession.Player1 = 'BOB' THEN GameSession.Player2
ELSE GameSession.Player1
END AS Opponent, Users.Rank
FROM GameSession
INNER JOIN Users
ON Users.Username=Opponent
WHERE GameSession.Player1 = 'BOB' OR GameSession.Player2 = 'BOB'
I’m using BOB here as a throwaway value.
The SQL error I’m getting through phpMyAdmin, which is where I’m doing my testing, is the following:
#1054 - Unknown column 'Opponent' in 'on clause'
I’m fairly confident I have my logic right but I’m having a very had time finding any information about the applicable Syntax for my situation. If anyone could give me a hand I’d really appreciate it, I’ve been scratching my head over this for hours. Thanks in advance 🙂
You will need to copy the entire expression: