sql 生成 table 足球比赛时间表(可变 2 人球队和所有可能的比赛)
sql generating table football's games schedule (variable 2-person teams & all possible games)
好的,我想问你一些类似的问题:Cross Join without duplicate combinations
我有 8 名 table 足球运动员。我们想玩所有可能的组合游戏。因此,为了生成所有可能的团队(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)
再试一次:
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
Returns 210 行!
我检查过:
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 & e VS a & f
结果中存在这个组合(f e d a - f & e VS d & a),但不是完全相同的游戏
我希望现在我能更清楚地解释我的问题。
好的,我想问你一些类似的问题:Cross Join without duplicate combinations 我有 8 名 table 足球运动员。我们想玩所有可能的组合游戏。因此,为了生成所有可能的团队(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)
再试一次:
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
Returns 210 行!
我检查过:
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 & e VS a & f
结果中存在这个组合(f e d a - f & e VS d & a),但不是完全相同的游戏
我希望现在我能更清楚地解释我的问题。