我正在用php创建学生信息系统,数据库如下所示
**studentId** **Math** **English** **Science** **className**
1 80 66 85 3p1
2 75 83 84 3p1
3 70 88 90 3p1
4 50 82 50 5p3
5 88 77 77 3p1
6 92 97 96 3p1
我想在使用php时只对className 3P1的成员进行排名。当包含以下内容时,下面的sql语句将在phpmyadmin中排名很好
SET @myclassName:=0;
SET @myRank:= 0;
SET @myNextRank:= 0;
SET @myTotals:= 0;
然而,当我将其转换为php并在php中使用它时,生成的等级是不一致的。每次我重新加载页面时,排名也会发生变化。 我如何包含
SET @myclassName:=0;
SET @myRank:= 0;
SET @myNextRank:= 0;
SET @myTotals:= 0;
在php的查询中。
<?php
$StudentMarkz = "SELECT X.*,
@myRank:=CASE WHEN @myclassName <> className
THEN @myNextRank:=1
WHEN @myTotals <> totals
THEN @myRank:=@myNextRank
ELSE @myRank
END AS Rank,
@myclassName:=className AS myclassName,
@myTotals:=totals AS myTotals,
@myNextRank:=@myNextRank+1 AS myNextRank
FROM ( SELECT studentmarkstable.studentId,surname, firstName,
Math,
English,
Science,
SocialStudies,
studentmarkstable.className,
Math + English + Science + SocialStudies AS totals
FROM studentmarkstable, student
WHERE student.studentId=studentmarkstable.studentId
AND studentmarkstable.className='3P1'
ORDER BY className ASC,
6 DESC
) X";
$marksQuery = mysql_query($StudentMarkz) or die (mysql_error());
?>
如果我使用$ marksQuery = mysqli_multi_query($ StudentMarkz)或die(mysql_error());我收到了一个错误。
提前谢谢
答案 0 :(得分:2)
这是PHP方式(当然你也可以使用mysql方式)。
$sql = "SELECT studentId, Math, Engish, Science FROM table";
$query = mysql_query($sql) or die("Request not successfull!");
$results = array();
while ($result = mysql_fetch_array($adressen_query)){
$results[ $result['studentId'] ] = $result['Math'] + $result['Engish'] + $result['Science'];
}
// results now contains all studentIds as Keys and all added result values as value.
// sort descending
$results = arsort( $results );
其他方法:
您可以在表格中添加一个新字段“全部”,并在那里总结所有结果。 (将结果写入表格时,您也可以总结'全部'字段) 然后,您可以使用查询和“ORDER BY”
来访问您的数据SELECT 'All' FROM table ORDER BY 'All' DESC;
答案 1 :(得分:2)
行。这是MySQL的解决方案,因为它可能是PHP脚本编写者中最常用的数据库:
SET @myClass:=0;
SET @myRank:= 0;
SET @myTotals:= 0;
SELECT X.*,
@myRank:=CASE WHEN @myClass <> class
THEN 1
WHEN @myTotals <> totals
THEN @myRank+1
ELSE @myRank
END AS Rank,
@myClass:=class AS myClass,
@myTotals:=totals AS myTotals
FROM ( SELECT studentId,
Math,
English,
Science,
Class,
Math + English + Science AS totals
FROM studentGrades
ORDER BY 5 ASC,
6 DESC
) X
给出:
studentId Math English Science Class totals Rank myClass myTotals
2 75 83 84 3p1 242 1 3p1 242
5 88 77 77 3p1 242 1 3p1 242
1 80 66 85 3p1 231 2 3p1 231
6 92 97 96 5p2 285 1 5p2 285
3 70 88 90 5p2 248 2 5p2 248
4 50 82 50 5p2 182 3 5p2 182
在队伍中没有差距
修改强>
如果您希望在几个同等级别的学生(例如1,2,2,4而不是1,2,2,3)之后保留后续的学生排名职位,请使用以下内容:
SET @myClass:=0;
SET @myRank:= 0;
SET @myNextRank:= 0;
SET @myTotals:= 0;
SELECT X.*,
@myRank:=CASE WHEN @myClass <> class
THEN @myNextRank:=1
WHEN @myTotals <> totals
THEN @myRank:=@myNextRank
ELSE @myRank
END AS Rank,
@myClass:=class AS myClass,
@myTotals:=totals AS myTotals,
@myNextRank:=@myNextRank+1 AS myNextRank
FROM ( SELECT studentId,
Math,
English,
Science,
Class,
Math + English + Science AS totals
FROM studentGrades
ORDER BY 5 ASC,
6 DESC
) X
给出
studentId Math English Science Class totals Rank myClass myTotals myNextRank
2 75 83 84 3p1 242 1 3p1 242 2
5 88 77 77 3p1 242 1 3p1 242 3
1 80 66 85 3p1 231 3 3p1 231 4
6 92 97 96 5p2 285 1 5p2 285 2
3 70 88 90 5p2 248 2 5p2 248 3
4 50 82 50 5p2 182 3 5p2 182 4
排名差距