【问题标题】:sql generating table football's games schedule (variable 2-person teams & all possible games)sql 生成桌上足球的比赛时间表(可变的 2 人团队和所有可能的比赛)
【发布时间】: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


【解决方案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 行!

【讨论】:

  • 谢谢,这似乎是正确的,但我要检查一下。 ;)
  • 所以我查了一下,我有 70 个结果,但我认为这还不够。
  • 数学,这对我来说已经是很多年前的事了……你期待多少场比赛?
  • 是的,这是个问题,因为我不太确定。我回答了我的问题,因为 cmets 不允许我进行足够的描述。
  • 现在看来真的没问题!非常感谢!
【解决方案2】:

我检查过:

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),但又不是完全一样的游戏

我希望现在我能更清楚地解释我的问题。

【讨论】:

  • 是的,我看到了问题所在。 (我不应该在星期六工作,尤其是星期六早上……我现在需要更多的咖啡!)
  • 210 种组合? (8*7/2) * (6*5/2) / 2
  • 我相信这是正确的数字,当我使用我的第一种方式生成时,我有 420 种组合,它似乎翻了一番,所以这真的是可能的。 :)
猜你喜欢
  • 2020-10-08
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
相关资源
最近更新 更多