Detect future duplicate values while iterating through MySQL results in PHP

后端 未结 3 839
我在风中等你
我在风中等你 2020-12-21 11:03

I am trying to calculate a ranking of a team in an ordered MySQL result set, and the issue I\'m having is detecting ties for the first team to show up with the tied

3条回答
  •  一个人的身影
    2020-12-21 11:41

    I like Ignacio's link to his answer. But if you still wanted to use PHP, you could collect the ranks by SCORE and assign teams to each score. It's probably not the most efficient way to do it, but it would work.

    $ranks = array();
    while ($row = mysql_fetch_assoc($result)) {
        $ranks[$row['pts']][] = $row['team_id'];
    }
    

    $ranks would be an array that could look like...

    $ranks[89] = array(1);
    $ranks[87] = array(2);
    $ranks[76] = array(3,4);
    $ranks[52] = array(5);
    

    Use a foreach on $ranks, and double check which way the points would come up (ascending or descending). You can use count() to see if there's a tie.

提交回复
热议问题