-2
$sum = 0;
$sqlM = "SELECT * FROM players";
$resM = mysql_query($sqlM) or die(mysql_error());
while($rowM = mysql_fetch_array($resM)){
//Total Runs
    $sqlr = "SELECT * FROM runs WHERE pId = ".$rowM['Id']."";
    $resr = mysql_query($sqlr) or die(mysql_error());
    while($rowr = mysql_fetch_array($resr)){
        $sum += $rowr['runs'];
    }
    $totalRuns[$rowr['pId']] = array(
         array( 
        'id' => intval($rowr['pId']),
        'score' => $sum
        )
    );


};

如何从每个玩家那里获得所有总跑数并将它们添加到数组中以进行排序?有 2 桌球员和跑垒,所以对于每个玩家,我需要他的总跑垒并将其添加到数组中,这样我就可以获得最好的 5 跑得分手。我尝试了我所知道的一切,但我没有做对。上面的代码是我到目前为止所做的,但它不起作用。

4

2 回答 2

0

为什么不像这样创建另一个数组?

$player;
$totolarun;
$player_total=array();

$sum = 0;
$sqlM = "SELECT * FROM players";
$resM = mysql_query($sqlM) or die(mysql_error());
while($rowM = mysql_fetch_array($resM)){
//Total Runs
        $sqlr = "SELECT * FROM runs WHERE pId = ".$rowM['Id']."";
        $resr = mysql_query($sqlr) or die(mysql_error());
        while($rowr = mysql_fetch_array($resr)){
                $sum += $rowr['runs'];
                $totolarun=$sum;
        }
        $player = $rowr['pId'];
        $player_total[$player]= $totolarun;

};
foreach($player_total as $key=>$value){
echo $key ." = ".$value." <br>"; 
}

var_dump($player_total);

希望它能给你一个想法。小编辑在这里;O

于 2013-09-29T12:52:05.517 回答
0

尝试以下查询:

SELECT players.id, COUNT(runs.pId) as runCount
FROM players
JOIN runs ON runs.pId = players.id
GROUP BY players.id
ORDER BY runCount DESC
LIMIT 5

没试过,所以可能有语法错误,如果有,请在评论中发表,以便我修复它。

编辑:(可能的)语法错误修复

于 2013-09-29T12:56:41.570 回答