I have a query that needs to be able to left join a field at the right place given the condition that the field is equal to a variable.
Something like:
Something like this may work
LEFT JOIN challengesRead
ON challenges.userID = CASE
WHEN challenges.userID = $var THEN challengesRead.userID
WHEN challenges.opponentID = $var THEN challenges.opponen END
It seems you want this:
LEFT JOIN challengesRead
ON CASE WHEN challenges.userID = $var
THEN challenges.userID
WHEN challenges.opponentID = $var
THEN challenges.opponentID
END = challengesRead.userID
which can be simplified to:
LEFT JOIN challengesRead
ON $var = challengesRead.userID
AND ( challenges.userID = $var
OR challenges.opponentID = $var
)