代码脚本,用于按另一个表的值对用户进行排序

时间:2018-03-12 23:39:58

标签: php mysql sql

我继承了一个似乎不按预期工作的脚本。 我有一个表格列出识别用户的ID号码,以及他们的积分和位置(基于最高点数)。

该脚本旨在查看用户与另一个用户具有相同点的用户,并根据列出单个事件结果的另一个表的值对其进行排序。打破平局是最高的位置。

E.g。 用户A和用户B并列第四,所有积分均为50分。然而,用户B对各种事件的最佳结果是第二名,而用户A是第三名。因此,脚本应将用户B放在第4位,将用户A放在第5位。 同样,如果两个用户都具有相同的最佳结果,则脚本会查看第二个最佳结果,依此类推。

这是我的代码,根据我的理解,应该工作(但事实并非如此)。

$ season_id,$ season_start,$ season_end,$ compId是之前声明的所有变量。

    $roundsarray = array();
$roundstotal=mysql_query("SELECT id FROM calendar WHERE eventDate BETWEEN '$season_start' AND '$season_end' AND competition='$compId' AND tag!='T' ORDER BY eventDate ASC");
$roundsnum = mysql_num_rows($roundstotal);
while($row = mysql_fetch_assoc($roundstotal)) {
    $roundsarray[]=$row['id'];
}

$datafirst=mysql_query("SELECT id FROM calendar WHERE eventDate BETWEEN '$season_start' AND '$season_end' AND competition='$compId' AND tag!='T' ORDER BY eventDate ASC LIMIT 1");
while($row=mysql_fetch_row($datafirst)) {   $firstevent = $row[0];  }

$datalast=mysql_query("SELECT id FROM calendar WHERE eventDate BETWEEN '$season_start' AND '$season_end' AND competition='$compId' AND tag!='T' ORDER BY eventDate DESC LIMIT 1");
while($row=mysql_fetch_row($datalast)) {    $lastevent = $row[0];   }

$racesessions = mysql_query("SELECT DISTINCT eventSession FROM results WHERE eventID>='$firstevent' AND eventID<='$lastevent' AND compId='$compId' AND eventSession NOT IN ('T', 'P', 'Q', 'Q1', 'Q2', 'QA')");
$racesnum = mysql_num_rows($racesessions);
$totalsessions = $racesnum*$roundsnum;

$points = "SELECT DISTINCT points FROM standings_drivers WHERE season='$season_id' ORDER BY points DESC";
$ptcom = mysql_query($points);
while ($row = mysql_fetch_row($ptcom)) {
    $points_pos = $row[0];

    $numpoints = "SELECT driver FROM standings_drivers WHERE season='$season_id' AND points='$points_pos'";
    $numpts = mysql_query($numpoints);
    $amount = mysql_num_rows($numpts);

    if ($amount==1) {}
    if ($amount>1) {

        $numpointsfirst = "SELECT driver, position FROM standings_drivers WHERE season='$season_id' AND points='$points_pos' ORDER BY position ASC LIMIT 1";
        $num1stpts = mysql_query($numpointsfirst);
        while ($row = mysql_fetch_row($num1stpts)) {
            $driver1_id = $row[0];
            $driver1_pos = $row[1];
        }
        $numpointslast = "SELECT driver, position FROM standings_drivers WHERE season='$season_id' AND points='$points_pos' ORDER BY position DESC LIMIT 1";
        $numlstpts = mysql_query($numpointslast);
        while ($row = mysql_fetch_row($numlstpts)) {
            $driverl_id = $row[0];
            $driverl_pos = $row[1];
        }


        $orderbit = "ORDER BY position1";
        $superquer = "SELECT SD.driver, 

             COALESCE((SELECT R.position FROM results R 
             WHERE R.driverId = SD.driver AND R.compId='$compId' 
             AND R.eventID>='$firstevent' AND R.eventID<='$lastevent' 
             AND R.eventSession NOT IN ('T', 'P', 'Q', 'Q1', 'Q2', 'QA') 
             ORDER BY R.position 
             LIMIT 1 OFFSET 0), 999) AS position1";

             $i=2;
             $max = $totalsessions+1;

            while ($i<$max) {

                $superquer .= ", COALESCE((SELECT R$i.position FROM results R$i 
                 WHERE R$i.driverId = SD.driver AND R$i.compId='$compId' 
                 AND R$i.eventID>='$firstevent' AND R$i.eventID<='$lastevent' 
                 AND R$i.eventSession NOT IN ('T', 'P', 'Q', 'Q1', 'Q2', 'QA') 
                 ORDER BY R$i.position 
                 LIMIT 1 OFFSET $i), 999) AS position$i";

                $orderbit .= ", position$i";

                $i++;
            }                


        $superquer .= " FROM standings_drivers SD 
        WHERE SD.season = '$season_id' AND SD.points='$points_pos'";

        $orderbit .= ";";

        $querytotl = "$superquer $orderbit";

        $sqres = mysql_query($querytotl);
        if($sqres === FALSE) { 
            die(mysql_error()); // TODO: better error handling
        }
        $n=0;
        while ($row = mysql_fetch_row($sqres)) {
            $driver_id = $row[0];

            $name = "SELECT CONCAT (firstName, ' ', lastName) AS username FROM users WHERE id='$driver_id'";
            $nameres = mysql_query($name);
            while ($row = mysql_fetch_row($nameres)) {
                $name=$row[0];
            }

            $pos=$driver1_pos+$n;

            $query12 = "UPDATE standings_drivers SET position='$pos' WHERE driver='$driver_id' AND season='$season_id'";
            mysql_query($query12);

            $n++;
        }
    }       
}

问题 - 脚本正确识别点上的用户级别并管理排序的第一步,但是当最高得分相同时,无法按正确的顺序排序。

我没有写原始的代码,我有点迷失了怎么办!任何帮助表示赞赏。

0 个答案:

没有答案