0

我正在尝试为足球数据库创建一个自动分数更新器。我的客户将通过电子邮件向我发送包含比赛日期、主队、主场比分、客队和客场比分的 CSV 电子表格。问题是我有一个单独的团队表(team_id、team_name 等)和游戏表,如下所述:

团队(team_id、team_name 等)

游戏(game_id、game_home_team、game_home_score、game_visitor_team、game_visitor_score、game_date、game_complete)

sample game data(1, 123, 0, 156, 0, 8-31-2012, No) -> 我在比赛表中存储了球队的 id 而不是他们的名字。

我需要编写一个脚本来自动更新分数。每周将有大约 327 个分数。

CSV 将如下所示(带有列标题):

game_date, home_team, home_score, visitor_team, visitor_score

(2012-8-31, Forest Hills, 44, Westmont Hilltop, 0) -> 样本数据

我将如何首先获得球队 ID,然后将他们的主场和访客 ID 与他们的球队名称相匹配,然后获取与匹配的 team_id 对应的游戏 ID,然后更新该游戏的分数?这是很多,我有点迷失在这个问题上。任何帮助甚至更好的方法都将不胜感激。以下是我到目前为止的内容:

UPDATE game SET 
    game_home_score = ?, 
    game_visitor_score = ?, 
    game_complete = 'Yes' 
WHERE game_id = 
    SELECT game_id 
    FROM game 
    WHERE game_home_team IN (
        SELECT team_id 
        FROM team 
        WHERE team_name = ?
    ) AND game_visitor_team IN (
        SELECT team_id 
        FROM team 
        WHERE team_name = ?
    );

我知道该查询是错误的,但就我需要走的方向而言,这是我的想法。我的 MySQL 知识还不够广博(我不这么认为)来完成这项工作。

4

1 回答 1

0

让我们将 CSV 文件中的数据放入一个名为 csv_data 的表中,这样应该可以:

update game g, csv_data c, team home_team, team visit_team
set g.game_home_score = c.home_score,
    g.game_visitor_score = c.visitor_score,
    g.game_complete = 'Yes'
where g.game_date = c.game_date
and home_team.team_name = c.home_team
and home_team.team_id = game.home_team
and visit_team.team_name = c.visit_team
and visit_team.team_id = game.visit_team

希望有帮助。

于 2012-09-06T16:00:32.693 回答