关于SQL的一个查询语句

来源:百度知道 编辑:UC知道 时间:2024/07/04 04:36:18
账号 昵称 | 账号 好友账号 好友分组
111 111 | 111 222 我的好友
222 222 | 111 333 陌生人
333 333
表A(users) 表B(groups)

要达到 查询1111的好友 结果显示为:

好友账号 昵称 所属分组
222 222 我的好友
333 333 陌生人
其中
select a.账号,昵称 from users a where a.账号 in (select b.好友账号 from groups b where b.账号=1111)
能查出所有的好友账号和昵称 现在查询的是好友账号 昵称 所属分组

select b.帐号,a.昵称,b.所属分组
from B b
left join A a on a.帐号=b.帐号
where a.帐号=1111
这样就行了吧

select groups.好友账号,users.昵称,groups.所属分组
from groups left join users on users.账号=groups.好友账号
where groups.账号='111'