这是对question I asked previously。
的跟进我可以计算等级(包括关系的逻辑)就好了;当我遇到重复排名的第一个实例时,问题是检测到未来的重复。
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;
team_id wins
--------------
10 52
2 48
5 46
11 46
3 42
9 39
...
$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给了我a potential solution,但我很想知道是否有更有效的方法(甚至可能在SQL级别)。
感谢。
答案 0 :(得分:1)
在伪代码中:
loop through rows as row1
loop through rows as row2
if row1 ain't row2 and row1.points == row2.points, append T
更新
好的,这是怎么回事,因为你总是按胜利排序你的结果集:尝试在临时数组或变量中存储关于每一行的信息,比如$previousTeamWins
,
$previousTeamName
等等。然后你可以比较当前和之前的,并根据它分配T.因此,您有效地将分配延迟到下一次迭代(或者直到最后一行的情况下循环退出)。通过行集的一次旅行应该完成工作。
答案 1 :(得分:0)
这个怎么样?
SELECT
t1.*,
EXISTS (
SELECT *
FROM `teams` as t2
WHERE t2.pts = t1.pts AND t1.id != t2.id
) as `tied`
FROM `teams` as t1
...
答案 2 :(得分:0)
试试这个我希望它为你工作
SELECT t1.`id`,t1.`pts`,t1.`team_id`,
IF(t1.`pts` = t2.`pts` AND t1.`id` != t2.`id` ,1,0) AS `tied`
FROM `teams` t1
LEFT JOIN `teams` t2 on t2.`pts` = t1.`pts` AND t2.`id` != t1.`id`
GROUP bY t1.`id`
答案 3 :(得分:0)
Oi,还在看你的代码。所以在一天结束时,你只想输出一两个团队,对吧?如果领导者是有问题的团队,那么就有一个,否则就是两个。如果是这种情况,请尝试这样的事情(警告它没有经过测试):
$i = 0;
while($row = mysql_fetch_assoc($r)) { //iterate thru ordered (desc) SQL results
++$i;
($row['wins'] == $prev_wins)
? $rnk = $prev_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 ($prev_team_id == $team_id) {
$arr_ranks['tp']['cat'] = 'Total Wins';
$arr_ranks['tp']['actual'] = number_format($prev_wins,1);
$arr_ranks['tp']['league_rank'] = $prev_rnk;
$arr_ranks['tp']['div_rank'] = $div_rnk;
if ($row['wins'] == $prev_wins)
{
$arr_ranks['tp']['tie'] = true;
}
else
{
$arr_ranks['tp']['tie'] = false;
}
break;
}
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_wins = $row['wins'];
$prev_team_id = $row['team_id'];
$prev_rnk = $rnk;
}
if ($prev_team_id == $team_id) {
$arr_ranks['tp']['cat'] = 'Total Wins';
$arr_ranks['tp']['actual'] = number_format($prev_wins,1);
$arr_ranks['tp']['league_rank'] = $prev_rnk;
$arr_ranks['tp']['div_rank'] = $div_rnk;
if ($row['wins'] == $prev_wins)
{
$arr_ranks['tp']['tie'] = true;
}
else
{
$arr_ranks['tp']['tie'] = false;
}
}
答案 4 :(得分:0)
我想我通过SQL找到了解决方案!可能不是很优雅(我可以稍后清理),但这里有......
查询:
SELECT a.team_id, a.wins, count(*) instances
FROM
(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) AS a
LEFT JOIN
(SELECT
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) AS b
ON a.wins = b.wins
GROUP BY a.team_id, b.wins
ORDER BY a.wins DESC;
这给出了输出......
=================================
|team_id | wins |instances |
=================================
|10 | 44 |1 |
|2 | 42 |3 | //tie
|9 | 42 |3 | //tie
|5 | 42 |3 | //tie
|3 | 41 |1 |
|11 | 40 |1 |
|... | | |
=================================
然后,在PHP中,我将通过检查$row['instances'] > 1
的时间来检测所有联系。
感谢大家通过这个非常麻烦的问题与我联系!