从表中熟悉的SQL视图

时间:2012-06-11 08:20:21

标签: sql

我有朋友的桌子

PLAYER_ID    FRIEND_ID
----------------------
1            2
1            3         // 1 knows 3
3            1         // 3 knows 1
2            4

如何为给定的玩家创建包含熟人(彼此认识)的视图?

PLAYER_ID    FRIEND_ID
----------------------
1            3
3            1
谢谢。

2 个答案:

答案 0 :(得分:2)

这是所需的选择:

select t1.PLAYER_id, t1.FRIEND_ID from tablename t1
inner join tablename t2 on t1.PLAYER_id = t2.FRIEND_ID and t2.PLAYER_id = t1.FRIEND_ID

答案 1 :(得分:2)

select p1.player_id, p1.friend_Id
from player p1 
inner join player p2 on p1.player_id = p2.friend_id and p2.player_id = p1.friend_id