SQL联接帮助列表中的朋友 [英] SQL join help for friend list

查看:46
本文介绍了SQL联接帮助列表中的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数据库表:usersuser_profilesfriends:

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

用户

  • id
  • 用户名
  • 密码

用户个人资料

  • id
  • user_id
  • 全名

朋友

  • id
  • usera_id
  • userb_id

查询将查找任何用户的朋友列表,并联接表usersuser_profiles以获得该朋友的个人资料和用户信息的查询是什么?

What would be a query which finds the friends list of any user and also joins the table users and user_profiles to get the profile and user information of that friend?

推荐答案

使用:

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 = ?

...假设userb_id是朋友ID.

...assuming userb_id is the friend id.

这篇关于SQL联接帮助列表中的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆