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

前端 未结 6 1378
情话喂你
情话喂你 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 06:51

    You can not use the IF THEN ELSE END IF-stuff in a SELECT in this way. However, you can use it in stored procedures and functions.

    I would JOIN u.id with both m.user1ID and m.user2ID and use DISTINCT to avoid duplicates.

    There is a IF() which you can use in SELECTs, but you can not do flow control with IF().

提交回复
热议问题