SQL join help for friend list

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

    Falling back on bad habits (not using JOIN notation in the FROM clause):

    SELECT a.id, a.username, a.full_name,
           b.id, b.username, b.full_name
      FROM friends AS f, users AS ua, users AS ub,
           user_profiles AS a, user_profiles AS b
     WHERE f.usera_id = ua.id
       AND f.userb_id = ub.id
       AND a.user_id  = ua.id
       AND b.user_id  = ub.id
    

    The key point is using table aliases (all those 'AS' clauses) and referencing the same table more than once when necessary.

    Someone could write this with JOIN instead.

提交回复
热议问题