以下是赛程表,其中显示了对阵的比赛(此处数字指的是球队 ID)。
team_1 | team_2
----+--------+--------
2 | 3
4 | 5
1 | 2
4 | 3
1 | 4
2 | 5
我需要获取与团队 2 的比赛,应该是 1,5,3 是否有任何查询可以帮助组合 team_1 和 team_2 并返回 1,5,3。我试过了
select * from fixtures where team_1 = 2 or team_2 = 2;
使用上面的方法,我可以得到与如下所示的 2 进行的比赛(I),但我需要在单列中显示 1、5、3,如(II)所示
(I) | team_1 | team_2
+--------+--------
| 2 | 3
| 1 | 2
| 2 | 5
(II) teams
-----
3
1
5
是否可以使用sql查询?
最佳答案
许多方法之一是使用 case
语句返回不是 team = 2 的列:
select case when team_1 = 2 then team_2 else team_1 end as opposing_team
from fixtures where team_1 = 2 or team_2 = 2;
这将返回:
opposing_team
3
1
5
关于sql - 在postgresql中形成子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29881617/