好的,我想问你一些类似的问题: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)
答案 0 :(得分:1)
另一次尝试:
select t1.id as t1p1,
t2.id as t1p2
from players t1
join players t2 on t1.id > t2.id
join (select t3.id as t2p1,
t4.id as t2p2
from players t3
join players t4 on t3.id > t4.id)
on t2p1 not in (t1.id,t2.id)
and t2p2 not in (t1.id,t2.id)
and t1.id > t2p1
返回210行!
答案 1 :(得分:0)
我查了一下:
select t1.id as t1p1,
t2.id as t1p2,
t3.id as t2p1,
t4.id as t2p2
from scores.players t1
join scores.players t2 on t1.id > t2.id
join scores.players t3 on t2.id > t3.id
join scores.players t4 on t3.id > t4.id
结果(70行):
t1p1 t1p2 t2p1 t2p2
------------------------------
d c b a
e c b a
e d b a
e d c a
e d c b
f c b a
f d b a
f d c a
f d c b
f e b a
f e c a
f e c b
f e d a
f e d b
f e d c
g c b a
g d b a
g d c a
g d c b
g e b a
g e c a
g e c b
g e d a
g e d b
g e d c
g f b a
g f c a
g f c b
g f d a
g f d b
g f d c
g f e a
g f e b
g f e c
g f e d
h c b a
h d b a
h d c a
h d c b
h e b a
h e c a
h e c b
h e d a
h e d b
h e d c
h f b a
h f c a
h f c b
h f d a
h f d b
h f d c
h f e a
h f e b
h f e c
h f e d
h g b a
h g c a
h g c b
h g d a
h g d b
h g d c
h g e a
h g e b
h g e c
h g e d
h g f a
h g f b
h g f c
h g f d
h g f e
所以我试图找到我们玩的第一个真正的游戏:
d e a f
意思是: d&amp; e VS a&amp; ˚F强>
这种组合存在于结果中(f e d a - f&amp; e VS d&amp; a ),但它不是完全相同的游戏
我希望现在我能更清楚地解释我的问题了。