Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据总分计算排名_Php_Mysql_Mysqli - Fatal编程技术网

Php 如何根据总分计算排名

Php 如何根据总分计算排名,php,mysql,mysqli,Php,Mysql,Mysqli,我有3个表,我想在一个表中显示基于主键、外键的3个表数据,结果非常完美!但我需要根据第二张表的总分计算排名 结果截图: 请告诉我计算排名的查询 我使用了以下mysql查询 if(isset($_POST['submit'])) { $result = mysqli_query($con," SELECT s.student_name , s.contact_number , m.total , m.rank , p.father_name FROM

我有3个表,我想在一个表中显示基于主键、外键的3个表数据,结果非常完美!但我需要根据第二张表的总分计算排名

结果截图:

请告诉我计算排名的查询

我使用了以下mysql查询

if(isset($_POST['submit']))
{
$result = mysqli_query($con,"
SELECT s.student_name
     , s.contact_number
     , m.total
     , m.rank
     , p.father_name 
  FROM student_details s 
  JOIN mark m
    ON s.student_id = m.student_id 
  JOIN parents_details p
    ON p.student_id = s.student_id 
 WHERE s.student_name = '".$_POST['student_name']."' 
");

echo "<table border='1' align='center' cellpadding='15' bgcolor='#FFFFFF'>
<tr>
<th>NAME</th>
<th>CONTACT NUMBER</th>
<th>TOTAL MARK</th>
<th>RANK</th>
<th>FATHER NAME</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['student_name'] . "</td>";
  echo "<td>" . $row['contact_number'] . "</td>";
  echo "<td>" . $row['total'] . "</td>";
  echo "<td>" . $row['rank'] . "</td>";
   echo "<td>" . $row['father_name'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);

}?>
if(isset($\u POST['submit']))
{
$result=mysqli\u查询($con,”
选择s.student\u名称
,s.联系电话
,m.total
,m.rank
,p.父亲的名字
来自学生的详细信息
连接标记m
关于s.student\u id=m.student\u id
加入家长计划详情p
在p.student\u id=s.student\u id上
其中s.student_name=“$”POST['student_name']。“'
");
回声“
名称
联系电话
总分
等级
父名
";
while($row=mysqli\u fetch\u数组($result))
{
回声“;
回显“$row['student_name']”;
回显“$行[“联系人号码]”;
回显“$row['total']”;
回显“$row['rank']”;
回显“$row['father_name']”;
回声“;
}
回声“;
mysqli_close($con);
}?>

请尝试上面的查询


在这里,我使用了
选择@rank:=0
@rank:=@rank+1

查看您的代码难以辨认,您的示例一行输出几乎无法让您的问题变得易懂。没有获得您想要的输出您想要基于升序或降序的排名吗?同样,请参阅关于准备好的和绑定的语句!我需要根据我在表格中的总分计算排名,并在表格中显示排名!但它显示排名7,但我的表中有4个数据!如何解决这个问题?只需提供您的示例数据。我在上面添加了示例数据屏幕截图,并执行了sql查询屏幕截图,但当我添加新数据时,它会给出错误的信息!请查找我的更新版本screenshot@M.Lak您只需尝试使用我更新的查询执行,
SELECT * FROM 
(
    SELECT @rank := @rank+1 finalrank,ZZ.* FROM
    (
        SELECT student_details.student_name, 
        student_details.contact_number, mark.total, 
        mark.rank, parents_details.father_name 
        FROM student_details 
        INNER JOIN mark ON student_details.student_id=mark.student_id 
        INNER JOIN parents_details ON parents_details.student_id=student_details.student_id ,(SELECT @rank:=0)z
        ORDER BY mark.total desc
    )ZZ
)ZZZ 
WHERE ZZZ.student_name = '".$_POST['student_name']."'