我希望在我的表mysql表'friends'中找到单向关系
Name Friend
John Mary
Krish Paula
Debbie John
Paula Krish
Mary John
我想列出所有没有配对(B - A)的配对(A - B) 示例:查询应选择第4行(Debbie - John),因为(John - Debbie)不存在。
答案 0 :(得分:4)
Select f1.* from Friends as f1
left join Friends as f2 on f1.Name = f2.Friend and f1.Friend = f2.Name
where f2.Name is null
答案 1 :(得分:4)
您也可以使用 greatest() and least()
Select
greatest(Name,Friend),least(Name,Friend)
from Friends
group by greatest(Name,Friend),least(Name,Friend)
having count(1)=1
<强> SQL FIDDLE 强>
答案 2 :(得分:1)
这是解决问题的查询。
select * from friends where name not in (select a.name from friends a, friends b where a.friend=b.name and a.name=b.friend);
答案 3 :(得分:-1)
Select Name, Friend from Friends as f1
left join Friends as f2 on f1.Name = f2.Friend AND f1.Friend = f2.Name