回 帖 发 新 帖 刷新版面

主题:两个表之间的嵌套查询!!急,有加分

现在有表 A,B如下:
表A(主键为ID自动编号的)
name(用户)  online(在线)
a             0
b             1
c             0
d             1
e             0
f             1
g             0
表B(主键为II自动编号)
name(用户)   friendname(好友)
a              b
a              c
a              d
b              a
b              g
b              f
b              e
我想要把 B表中的 name为‘a’中friendname 的online为1的用户选出来,理论上a的好友有b、c、d,而符号要求的在线的只有b、d。。
但是我不知道如何用SQL语句来实现,请大家帮帮忙。。急用!!

回复列表 (共3个回复)

沙发

我用以下的试了一下。。可以了。。
select A.name
from A,B
where  B.name='a' and A.online='1' and A.name=B.name;
结果显示是:
b
d
[em1]

板凳

select name from A where online=1 and name in(select friendname from B where name='a')嵌套
连接,
select B.frendname from A inner join B on B.name='a' and A.name=B.friendname and A.online=1
不知道对不对,没试过,好久没用过SQL,有点生了
兄弟自已试试

3 楼

试试下面的
select A.name
from A,B
where  B.name='a' and A.online='1' and A.name=B.friendname;

我来回复

您尚未登录,请登录后再回复。点此登录或注册