简体   繁体   English

A喜欢B,但是B不喜欢A?

[英]A likes B, but B does not like A?

If my table, called Table, looks like this: 如果我的表名为Table,则如下所示:

A B
1 2
3 2
2 1
etc...

This would imply 1 likes 2, 3 likes 2, and 2 likes 1...assuming its a lot bigger than this, how do I write an SQL query where I report where A likes B, but B does not like A? 这意味着1喜欢2、3喜欢2和2喜欢1 ...假定它比这个大很多,如何编写一个SQL查询来报告A在哪里喜欢B,但B不喜欢A?

So for this case, an example output should be: 因此,在这种情况下,示例输出应为:

3 2

Because 3 likes 2 from the relation, but 2 does not like 3. 因为3从关系中喜欢2,但是2不喜欢3。

SELECT A, B
  FROM likes x
 WHERE NOT EXISTS (SELECT * FROM likes
                    WHERE x.A = B AND x.B = A)

I think you just need to use a left join (Disclaimer: I use SQL Server not postgresql) 我认为您只需要使用left join联接(免责声明:我使用SQL Server而不是postgresql)

select l.A, l.B
from likes l
  left join likes nl on l.B=nl.A and l.A=nl.B
where nl.A is null 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM