Oracle SQL比较值

用户名

在具有用户和朋友表的关系数据库中,我试图确定成对的异性用户是朋友。因此,用户a可能会与用户b成为朋友,但反之则不然。我想查找所有实例,其中a是b的朋友,b也是a的朋友。我有一个查询,该查询将所有单向的友谊返回给异性,但不确定如何进一步减少答案以取出不匹配的对。

这是表的结构:

Table: users
Attributes: id, name, gender
Table: friends
Attributes: id1, id2, startdate
杰弗里·坎普(Jeffrey Kemp)

为了找到所有相互的MF友谊,假定性别始终为M或F:

select distinct a.name, a.id, b.name, b.id
from users a, users b, friends f1, friends f2
where f1.id1 = a.id
and f1.id2 = b.id
and a.gender != b.gender
and f2.id1 = b.id
and f2.id2 = a.id;

或者,使用ANSI连接语法:

select distinct a.name, a.id, b.name, b.id
from   friends f1
join   friends f2 on f1.id1 = f2.id2 and f1.id2 = f2.id1
join   users a    on a.id = f1.id1
join   users b    on b.id = f1.id2
where  a.gender != b.gender;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章