2017-05-31 31 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