-1

I am looking to find one way relationships in my table mysql table 'friends'

Name    Friend
John    Mary
Krish   Paula
Debbie  John
Paula   Krish
Mary    John

I want to list all pairs (A - B) for which no pair (B - A) can be found.
Example: The query should select row 4 (Debbie - John) since (John - Debbie) does not exist.

4

4 回答 4

4

您也可以使用最大()和最小()

Select 
greatest(Name,Friend),least(Name,Friend)
from Friends
group by greatest(Name,Friend),least(Name,Friend)
having count(1)=1

SQL 小提琴

于 2013-07-04T08:05:24.457 回答
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

Sql 小提琴

于 2013-07-04T07:57:15.517 回答
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);
于 2013-07-04T08:13:26.133 回答
-1
Select Name, Friend from Friends as f1
left join Friends as f2 on f1.Name = f2.Friend AND f1.Friend = f2.Name
于 2013-07-04T08:02:57.863 回答