日期:2014-05-18  浏览次数:20619 次

请教平级关系关联表的设计
我要设计一个表,比如是好友关系表,关系是这样的,我是A,我和B是好友关系,B和C是好友关系,当我A查看自己好友的时候,会把B、C都带出来显示,当C查看的时候会把B、A带出来显示,请问这个表怎样设计便于查询啊?以及如何查询才比较科学一点呢?好友间并没有层级的关系。谢谢。

------解决方案--------------------
SQL code

create table friends(person varchar(5),friend varchar(5));
insert friends
select 'A','B' union all
select 'B','A' union all
select 'B','C' union all
select 'C','B'


select * from
(select f1.person as f1person,f1.friend as f1friend,f2.friend  as f2friend
from friends f1,friends f2
where f1.friend = f2.person) t1
where t1.f1person != t1.f2friend ;