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

前端 未结 6 1366
情话喂你
情话喂你 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:41

    The image will help you to find the structure of db..$userId is Uses id of user we wish to see the friend list for.

    SELECT `connections`.*,`usr`.*,"'.$existImage.'" as main_image_url,"'.$existThumb.'" as thumb_image_url FROM `connections` `connections` JOIN `users` `usr` ON `usr`.`userId` = (CASE WHEN `connections`.`requestBy`="'.$userId.'" THEN connections.requestFor ELSE connections.requestBy END) WHERE `connections`.`requestBy` = "'.$userId.'" OR `connections`.`requestFor` = "'.$userId.'" AND `connections`.`requestStatus` = 1
    

提交回复
热议问题