正如我的信息所述,我继承了本地飞镖联盟的统计学家工作。 (哇哦,一季 20 美元)
我很乐意实现 ELO 评级系统。
当前数据库表有超过 100,000 个条目。
共有 3 种不同的游戏。单打、 double 和团体赛。
数据库的创建者输入的游戏是这样的:
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)
这样做是为了方便个人的查找。
一场 double 比赛的值(value)如下
(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>
最佳答案
(我将其放入一个答案中,以便我可以更好地布局它 - 但它并不是真正的“解决方案”!)
我想我要做的是尝试构建一个按游戏分组的 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
的第一场单打比赛您的示例 - 无论我们查看的是哪种游戏记录。
这有意义/有帮助吗?
关于php - 我需要一些帮助来创建查询和数据库表以追溯地将数字分配给现有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20391745/