这是我之前提出的问题的后续。
我可以很好地计算排名(包括平局的逻辑);当我遇到重复排名的第一个实例时,问题是检测未来的重复。
这是获取结果集的 SQL 查询:
SELECT
s1.team_id,
sum(s1.score>s2.score) wins
FROM scoreboard s1
LEFT JOIN scoreboard s2
ON s1.year=s2.year
AND s1.week=s2.week
AND s1.playoffs=s2.playoffs
AND s1.game_id=s2.game_id
AND s1.location<>s2.location
GROUP BY s1.team_id
ORDER BY wins DESC;
这是我将在 PHP 中循环的示例 SQL 结果集:
team_id wins
--------------
10 52
2 48
5 46
11 46
3 42
9 39
...
这是我用于显示的 PHP 代码,它需要将“T-”附加到所有绑定的等级:
$i = 0;
while($row = mysql_fetch_assoc($r)) { //iterate thru ordered (desc) SQL results
++$i;
($row['wins'] == $prev_val)
? $rnk = 'T-' . $rnk //same as previous score, indicate tie
: $rnk = $i; //not same as previous score
$rnk = str_replace('T-T-','T-',$rnk); //eliminate duplicative tie indicator
if ($row['team_id'] == $team_id) { //current team in resultset matches team in question, set team's rank
$arr_ranks['tp']['cat'] = 'Total Wins';
$arr_ranks['tp']['actual'] = number_format($row['wins'],1);
$arr_ranks['tp']['league_rank'] = $rnk;
$arr_ranks['tp']['div_rank'] = $div_rnk;
}
else if ($i == 1) { //current team is category leader (rank=1) and is not team in question, set current team as leader
$arr_ranks['tp']['leader'] = "<a href='index.php?view=franchise&team_id=" . $row['team_id'] . "'>" . get_team_name($row['team_id']) . '</a> (' . number_format($row['wins']) . ')';
}
$prev_val = $row['wins']; //set current score as previous score for next iteration of loop
}
上面的“平局”逻辑将把#4队与#3队平局,但反之则不然。
换句话说,对于#3 队,$rnk = 3
,而对于#4 队,$rnk = T-3
。(两者都应该是“T-3”。)
所以问题就变成了:我如何在遍历结果时“向前看”以找出当前分数是否与列表后面的分数相同/重复,这样我就可以将它与随后的欺骗一起视为并列?
@Airzooka 给了我一个潜在的解决方案,但我很想知道是否有更有效的方法来做到这一点(甚至可能在 SQL 级别)。
谢谢。