How to use a case statement to determine which field to left join on

后端 未结 2 1878
独厮守ぢ
独厮守ぢ 2021-01-05 12:31

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:



        
2条回答
  •  生来不讨喜
    2021-01-05 13:00

    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
    

提交回复
热议问题