90球宾果游戏 - 获胜门票的最短途径

时间:2016-10-07 20:55:19

标签: mysql

对于90球宾果游戏引擎,我知道所有门票的外观,我需要根据给定游戏调用的数字来计算获胜者票的最短路径。

对于每场比赛,我们预计会产生大约1000张门票,每场比赛将有三场比赛:" 1行"," 2行" (显然是一张票)和#34;全票"。

路径被定义为我需要为获得优胜者而调用的号码 - 用于特定比赛。

表格布局非常简单:

IDisposable

每场比赛也会注册被叫号码。

票: ID game_id

ticket_row: ID TICKET_ID

ticket_row_field: ID ticket_row_id 含量

CALLED_NUMBER: game_id CALLED_NUMBER

所有字段都是整数类型。

我发现很容易获得比赛的最短路径" 1 row"和#34;全票"我可以分别通过ticket_row_id和ticket_id进行分组。

以下是我为1排比赛做的事情:

A ticket belongs to a game - it has three rows
A ticket row belongs to a ticket - it has 5 fields
A ticket row field belongs to a ticket row - it has one content field

我的问题是:如何在同一张票上找到比赛的最短路径" 2行"使用MySQL(不支持窗口函数)?

用更多技术术语来描述:对于每张票,我想找到与called_number不匹配的字段内容的总数,但仅限于缺少字段数量最少的两行。应该订购门票,以便那些缺少最少字段的门票首先出现。

我希望结果如下:

select 
    r.ticket_id, 
    count(content) path_distance, 
    group_concat(content order by content) path
from 
    ticket t
    inner join ticket_row r
        on r.ticket_id = t.id
    inner join ticket_row_field f
        on f.ticket_row_id = r.id
    left join called_number n
        on n.game_id = t.game_id
        and n.called_number = f.content
where
    n.called_number is null
group by 
    ticket_row_id
having 
    path_distance < 4
order by 
    path_distance;

1 个答案:

答案 0 :(得分:0)

以下似乎是要走的路: 首先,我们创建一些视图,以使实际的选择查询更容易理解

第一个视图表示已经完全填充的行 - 这意味着还调用了所有5个数字。

create or replace view filled as
select 
    r.ticket_id, f.ticket_row_id, 0 path_distance
from 
    ticket_row_field f
    inner join ticket_row r
        on r.id = f.ticket_row_id
    inner join ticket t
        on t.id = r.ticket_id
    inner join called_number n
        on n.called_number = f.content
group by ticket_row_id
having count(*) = 5;

另一个视图表示未完全填充的行

create or replace view incomplete as
        select 
            r.ticket_id, f.ticket_row_id,
                coalesce(filled.path_distance, count(content)) path_distance,
                case when 
                    filled.path_distance is not null then 
                    '' else 
                    group_concat(content order by content) 
                end path
        from 
            ticket t
            inner join ticket_row r
                on r.ticket_id = t.id
            inner join ticket_row_field f
                on f.ticket_row_id = r.id
            left join called_number n
                on n.game_id = t.game_id
                and n.called_number = f.content
            left join filled
                on filled.ticket_row_id = f.ticket_row_id
        where 
            n.called_number is null or filled.ticket_row_id is not null
        group by 
            ticket_row_id;

然后我们使用以下选择查询来获取我们需要的票证,包括它们的路径(=缺少的数字)

select 
    r1.ticket_id, r1.path_distance + r2.path_distance distance_sum, concat(r1.path, ',', r2.path) path_combined
from 
    incomplete r1
        inner join incomplete r2
            on r2.ticket_id = r1.ticket_id
where 
    r2.ticket_row_id < r1.ticket_row_id
order by 
    distance_sum
limit 5;