gpt4 book ai didi

php - 计算用户的排名

转载 作者:行者123 更新时间:2023-11-29 23:52:06 25 4
gpt4 key购买 nike

我想使用 MySQL 计算排名。我有一个名为结果的表

result_id    test_id    exam_id    user_id    percentage
1 5 6 50 57
2 5 6 58 76
3 5 6 65 42

我想根据用户的 user_id 和 test_id 计算用户的排名,例如 user_id(58) 有 1 排名 user_id(50) 有 2 等等

我尝试过查询

select percentage  from result where test_id=$test_id(i.e 5) and user_id=$user_id(i.e 58)

但它给出了 76 并且没有给出排名

我也尝试过

select percentage from result where test_id=$test_id(i.e 5) 

但它给了我 57,76,42

有什么方法可以计算用户的排名吗?

最佳答案

您可以简单地使用ORDER BY百分比DESC。试试这个...

$con = mysqli_connect("host","user","password","db_name");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = 'SELECT * FROM result ORDER BY percentage DESC';
$result = mysqli_query( $con, $sql );
if (!mysqli_query($con, $sql)) {
die('Error: '. mysqli_error($con));
}
$i = 1;
while($row = mysqli_fetch_array($result)) {
echo 'User ID ' . $row['user_id'] . ' has rank ' . $i . ' and percentage ' . $row['percentage'] . '</br>';
$i++;
}
mysql_close($con);

关于php - 计算用户的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580723/

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