team1
+----------+----------+
| id1 | name |
+----------+----------+
| 1 | a |
| 2 | b |
| 3 | c |
+----------+----------+
team2
+----------+----------+
| id2 | name |
+----------+----------+
| 1 | d |
| 2 | e |
| 3 | f |
+----------+----------+
play
+----------+----------+----------+
| id3 | id1 | id2 |
+----------+----------+----------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
| 4 | 3 | 2 |
| 5 | 2 | 3 |
+----------+----------+----------+
我是sql的新手。我正在努力设置计数条件。让我们对上面的表格说,我想找到一个来自team1的玩家,他和一个已经玩过或将至少玩过两次的team2的玩家一起玩。所以基本上,如果来自id2的值出现不止一次,那么来自id1的任何值都应该是结果的一部分。只要查看表格,我希望它返回id1 = 1,如果没有冗余,则返回id1 = 2。我无法绕过如何解决这个问题。
答案 0 :(得分:0)
尝试:
select distinct p1.id1
from play p1
join (select id2
from play
group by id2
having count(*) > 1) p2
on p1.id2 = p2.id2