SQL join help for friend list

后端 未结 4 1210
猫巷女王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:19

    Use:

    SELECT f.username,
           up.*
      FROM USERS f
      JOIN USER_PROFILES up ON up.user_id = f.id
      JOIN FRIENDS fr ON fr.userb_id = f.id
      JOIN USERS u ON u.id = fr.usera_id
     WHERE u.username = ?
    

    ...assuming userb_id is the friend id.

提交回复
热议问题