【发布时间】:2015-04-18 16:49:05
【问题描述】:
好的,我想问你一些类似的问题:Cross Join without duplicate combinations 我有 8 名桌上足球运动员。我们想玩所有可能的组合游戏。因此,为了生成所有可能的球队(2 名球员),我可以使用这个解决方案(我认为 28 支球队是正确的):
select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a join scores.players b on a.id!=b.id
但是我怎样才能在所有可能的团队之间生成所有可能的游戏而不重复呢?我不知道如何比较所有列。我尝试使用这个查询,结果是 420 个组合,但在我看来太多了:
select distinct
t1.p1 as t1p1,
t1.p2 as t1p2,
t2.p1 as t2p1,
t2.p2 as t2p2
from
(select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a join scores.players b on a.id!=b.id) t1
join
(select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a
join scores.players b on a.id!=b.id) t2 on (t1.p1!=t2.p1 and t1.p1!=t2.p2 and t1.p2!=t2.p1 and t1.p2!=t2.p2)
【问题讨论】:
-
是学校作业吗?
-
不,我正在尝试为我和我的朋友们制作自己的评分系统;)抱歉,也许这就像作业一样简单,但我不知道该怎么做。
-
如果从 a.id!=b.id 更改为 a.id
-
完全一样的结果。
-
您有一张桌子,玩家,有 8 行(每个玩家一个)?现在你想要所有可能的游戏?两支球队,每队两名球员?
标签: sql join permutation