SQL join help for friend list

后端 未结 4 1244
猫巷女王i
猫巷女王i 2021-01-18 18:41

I have three database tables: users, user_profiles and friends:

users

  • id
  • username
4条回答
  •  天命终不由人
    2021-01-18 19:13

    This may not be the best way to do it, but this felt like the logical way:

    select a.id , a.friend_id ,
    Users.username
    from
     ( SELECT id , IF(usera_id = 1, userb_id , usera_id) friend_id
     FROM friends
     where usera_id = 1 OR userb_id = 1 ) a
    left join Users on a.friend_id = Users.id
    

    this uses a mySQL function so probably wont work in Oracle/MSSQL

提交回复
热议问题