How do I use an IF statement in an MySQL join query?

前端 未结 6 1442
情话喂你
情话喂你 2021-02-04 06:09

I have a SQL query that left joins a table in different ways depending on a condition.

SELECT m.id, u.first_name AS otherUser
FROM matches AS m
IF (u.id=m.user2I         


        
6条回答
  •  不要未来只要你来
    2021-02-04 07:00

    You can also have two LEFT JOIN on the same table like this...

    LEFT JOIN users AS u ON u.id = m.user1ID
    LEFT JOIN users AS u2 ON u2.id = m.user2ID
    

    This is an alternative to using IF statements.

提交回复
热议问题