我需要一些帮助来创建查询和数据库表来追溯地为现有表分配数字

时间:2013-12-05 04:54:55

标签: php mysql

正如我的信息所说,我继承了当地飞镖联盟的统计学家的工作。 (哇哇一个赛季20美元)

我很乐意实施ELO评级系统。

当前数据库表有超过100,000个条目。

有3种不同的游戏。单打,双打和团队。

数据库的发起人输入的游戏如下:

Index   Player1_num   Player2_num   Opp_Player1_num  Odd_Player2_num   H_team  V_team Season     Week   W   L  Game_type

两次进入单打比赛。

values(1,20,null,30,null,200,300,11,2,1,0,301_singles)

values(2,30,null,20,null,200,300,11,2,0,1,301_singles)

这样做是为了方便个人查看。

双打游戏会有诸如

之类的值
(3,20,21,30,31,200,300,11,2,1,0,501_doubles)
(4,21,20,30,31,200,300,11,2,1,0,501_doubles)
(5,30,31,20,21,200,300,11,2,0,1,501_doubles)
(6,31,30,20,21,200,300,11,2,0,1,501_doubles)

再一次,它是为了更容易查询查询各个季节和不同合作伙伴的胜负而构建的。

团队游戏是:

(7,null,null,null,null,200,300,11,2,1,0,team_game)

所以我在表格中添加了一个match_num列,以帮助将游戏归为1个数字。

但是,我不确定如何最好地分配数字,可以理解的是,我不想手动浏览所有90k条目。

注意:早期的季节是手工修改的,并非所有的双重匹配都有4个条目(有些只有1个)。有些匹配也可能不正常(而不是索引:1,2可能是1,9)。

我不知道您是否需要更多信息,我不确定如何发布更大的表格示例。

Current code: 
<body>
<?php
include "inc.php";
// Select Max Match Number
        $sqlMatch="select max(match_num) from stats_results";
        $match =mysql_query($sqlMatch);
        $m= $match ? mysql_result($match,0):mysql_error();
        $matchno= $m+1; // First Match number

$game=array("'301_singles'","'cricket_singles'","'501_singles'","'301_doubles'","'cricket_doubles'");


// selects max season
    $sqlSeason="select max(season_index) from stats_season";
    $season =mysql_query($sqlSeason);
    $smax= $season ? mysql_result($season,0):mysql_error();
#          $smax=2; 
// for each season up to  max season
            for($s=1;$s<=$smax;$s++)
                    {

// Selects max week within the current season
                    $sqlWeek="select max(week) from stats_results where season=$s ";
                    $Week =mysql_query($sqlWeek);
                    $wmax= $Week ? mysql_result($Week,0):mysql_error();
#                        $wmax=2;
// for each week within current season up to the max week
                    for ($w=1;$w<=$wmax;$w++)
                            {

// each singles game
                            foreach($game as $g)
                                    {

###########################################################################################################



$result = mysql_query("SELECT * FROM stats_results where season=$s and week=$w and game_code=$g ;") or die(mysql_error());

// Put them in array
for($i = 0; $rows[$i] = mysql_fetch_assoc($result); $i++) ;

// Delete last empty one
 array_pop($rows);
//******************************************************

$matches=array();
foreach($rows as $record)
{
    // Get a unique match code for this match
    $matchid= getMatchID($record);
    // Have we seen this ID before? If yes add this record index to the existing array
    // otherwise create an array with just this value
    if (!isset($matches[$matchid])) $matches[$matchid]= array($record['index_results']); // No
    else $matches[$matchid][]= $record['index_results']; // Yes, add this Index
}
// Now $matches is an array of arrays of Index values, grouped by "match" so...

/* Sort $matches by key (see getMatchID for why!) */
ksort($matches);

// Update the table 
foreach($matches as $match) 
{

    // Create SQL instruction to set the match_num for all the Index values in each entry
       $sql= "UPDATE stats_results SET match_num = $matchno WHERE index_results IN (".implode(",", $match).")";
echo "<br>";
echo $sql;
/* Execute the SQL using your chosen method! */

// Move the match count on
$matchno++;
}
// End our loops
                                    }
                            }
                    }

function getMatchID($gamerecord) 
{
$index= "{$gamerecord['season']}-{$gamerecord['week']}-{$gamerecord['game_code']}-";


$players= array(
   $gamerecord['player1_num'], 
   empty($gamerecord['player2_num'])?0:$gamerecord['player2_num'], 
   $gamerecord['opp_player1_num'], 
   empty($gamerecord['opp_player2_num'])?0:$gamerecord['opp_player2_num']
); 
// Sort the players to get them in a consistent order
sort($players);

// Add the sorted players to the index
$index.= implode('-', $players);
return $index;
}


?>
</body>

1 个答案:

答案 0 :(得分:4)

(我把它放到一个答案中,所以我可以让它更好地布局 - 但它并不是真正的“解决方案”!)

我认为我要做的是尝试构建一个按游戏分组的Index值数组数组 - 然后您可以使用它来创建SQL来更新表。

因此,如果$rows是所有记录的数组,我们会执行类似以下伪代码的操作:

$matches= array();

foreach($rows as $record)
{
    // Get a unique match code for this match
    $matchid= getMatchID($record);
    // Have we seen this ID before? If yes add this record index to the existing array
    // otherwise create an array with just this value
    if (!isset($matches[$matchid])) $matches[$matchid]= array($record['Index']); // No
    else $matches[$matchid][]= $record['Index']; // Yes, add this Index
}

// Now $matches is an array of arrays of Index values, grouped by "match" so...

/* Sort $matches by key (see getMatchID for why!) */
ksort($matches);

// Update the table 
$matchno= 1; // First Match number
foreach($matches as $match) 
{
   // Create SQL instruction to set the match_num for all the Index values in each entry
   $sql= "UPDATE games SET match_num = $matchno WHERE Index IN (".implode(",", $match).")";

   /* Execute the SQL using your chosen method! */

   // Move the match count on
   $matchno++;
}

所有离开的是getMatchID函数 - 如果我们根据每个匹配的季节,星期以及它的参与者的排序列表(并且首先使用季节和周)给每个匹配一个临时ID,它应该是唯一的对于每个游戏,我们可以稍后按此索引进行排序,以便以正确的顺序获得游戏。所以再次粗略的伪代码,如:

function getMatchID($gamerecord) 
{
   $index= "{$gamerecord['Season']}-{$gamerecord['Week']}-{$gamerecord['H_Team']}-{$gamerecord['V_Team']}-";

   $players= array(
       empty($gamerecord['Player1_Num'])?0:$gamerecord['Player1_Num'], 
       empty($gamerecord['Player2_Num'])?0:$gamerecord['Player2_Num'], 
       empty($gamerecord['Opp_Player1_Num'])?0:$gamerecord['Opp_Player1_Num'], 
       empty($gamerecord['Opp_Player2_Num'])?0:$gamerecord['Opp_Player2_Num']
   ); 

   // Sort the players to get them in a consistent order
   sort($players);

   // Add the sorted players to the index
   $index.= implode('-', $players);

   return $index;
}

所以一切顺利$index会为你的例子中的第一场单打比赛提供类似11-2-200-300-0-0-20-30的内容 - 无论我们在哪个比赛记录中查看。

这是否有意义/帮助?