gpt4 book ai didi

php - MySQL数据库选择运动员数据需要位置

转载 作者:行者123 更新时间:2023-11-29 18:53:17 25 4
gpt4 key购买 nike

我正在从数据库中选择数据并将其显示在页面上。
$position 是一个未保存在数据库中的有用值。 。显示每个运动员的位置很有用,如图所示,我只显示递增的 $i值(value)。

代码如下ORDERS BY run points 。问题是当按其他内容对列表进行排序时,例如 ORDER BY 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 `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列表中第一个名字以 A 开头的运动员如果不是得分最多的运动员,则为 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"

最佳答案

只需在循环外分配 $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

关于php - MySQL数据库选择运动员数据需要位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com