2017-05-31 97 views
0

我从数据库中选择数据并将其显示在页面上。
未保存在数据库中的一个有用值是$position。 显示每个运动员的位置很有用,这里显示的数字只是显示增加的$i值。

代码如下ORDERS BY run points。问题是,当按照其他事物排序列表时,例如ORDER BY athlete ASC,分配给每个运动员的位置不随运动员移动。选择运动员数据的MySQL数据库需要位置

$selectproduct = mysqli_query($link, "SELECT `athlete`,`reference`,SUM(`Run Points`) AS 'Run Points',COUNT(`eventID`) AS 'num_runs',`Volunteer Points`,`location`,`Gender pos` FROM `Events` WHERE `Gender`='$Gender' AND `location`='$location' GROUP BY `reference` ORDER BY `Run Points` DESC"); 


while ($rowGetDetails = mysqli_fetch_array($selectproduct)){ 

$reference=$rowGetDetails['reference']; 
$athlete=$rowGetDetails['athlete']; 
$points=$rowGetDetails['Run Points']; 
$num_runs=$rowGetDetails['num_runs']; 
$position=$i; 

echo '<tr>'; 
echo'<td>'; 
echo $athlete; 
echo '</td>'; 
echo'<td>'; 
echo $num_runs; 
echo '</td>'; 
echo'<td>'; 
echo $points; 
echo '</td>'; 
echo '</tr>'; 

$i=$i+1; 
} 

当别的东西排序,如athlete ASC$selectproduct它仍然给$postition作为该列表中的第一位运动员与一个名称数量1一开始的时候,是不是得分最多运动员。

还有其他选项$selectproduct如下面通过athlete ASC排序:

$selectproduct = mysqli_query($link, "SELECT `athlete`,`reference`,SUM(`Run Points`) AS 'Run Points',COUNT(`eventID`) AS 'num_runs',`Volunteer Points`,`location`,`Gender pos` FROM `Events` WHERE `Gender`='$Gender' AND `location`='$location' GROUP BY `barcode` ORDER BY `athlete` ASC" 
+0

我觉得你的查询看起来不错 – JYoThI

+0

您正在通过跑分排序 – maSTAShuFu

+0

谢谢你,当'排序的查询工作正常运行点数“,但在排序时,如”ORDER BY athlete ASC“等其他排序,运动员的位置不会移动。我将编辑我的问题以使其更清晰 – Jeanclaude

回答

0

只要分配$ i值外循环;

$selectproduct = mysqli_query($link, "SELECT `athlete`,`reference`,SUM(`Run Points`) AS 'Run Points',COUNT(`eventID`) AS 'num_runs',`Volunteer Points`,`location`,`Gender pos` FROM `Events_08052017_withdate` WHERE `Gender`='$Gender' AND `location`='$location' GROUP BY `reference` ORDER BY `Run Points` DESC"); 
    $i = 1; 

    while ($rowGetDetails = mysqli_fetch_array($selectproduct)){ 

    $reference=$rowGetDetails['reference']; 
    $athlete=$rowGetDetails['athlete']; 
    $points=$rowGetDetails['Run Points']; 
    $num_runs=$rowGetDetails['num_runs']; 
    $position=$i; 

    echo '<tr>'; 
    echo'<td>'; 
    echo $athlete; 
    echo '</a>'; 
    echo '</td>'; 
    echo'<td>'; 
    echo $num_runs; 
    echo '</td>'; 
    echo'<td>'; 
    echo $points; 
    echo '</td>'; 
    echo '</tr>'; 

    $i=$i+1; 
    } 

尝试查询这样的事情可能是其作品对您

SET @rank=0; 
SELECT @rank := @rank + 1 AS ranking, t.avg, t.name 
    FROM(SELECT avg(students_signatures.score) as avg, students.name as name 
FROM alumnos_materia 
JOIN (SELECT @rownum := 0) r 
left JOIN students ON students.id=students_signatures.id_student 
GROUP BY students.name order by avg DESC) t 
+0

谢谢您的回答。我刚刚尝试过,但它仍然无法正常工作。当在'$ selectproduct'中用其他东西比如'运动员ASC'进行排序时,如果列表中的第一个运动员名字以A开始,那么它仍然会给出'$ postition'作为第一名,当那个运动员不是点数最多的运动员时。再次感谢您的建议! – Jeanclaude