CREATE TABLE IF NOT EXISTS jss1_prelim ( id int(11) NOT NULL AUTO_INCREMENT, term varchar(30) NOT NULL, idnumber varchar(30) NOT NULL, subject varchar(30) NOT NULL, score varchar(30) NOT NULL, teacher varchar(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
jss1_prelim
id
term
idnumber
subject
score
teacher
INSERT INTO `jss1_prelim` (`id`, `term`, `idnumber`, `subject`, `score`, `teacher`) VALUES (1, 'Prelim', 'SBI-JS202', 'Computer', '82', 'TID273'), (2, 'Prelim', 'SBI-JS645', 'Computer', '63', 'TID273'), (3, 'Prelim', 'SBI-JS300', 'Computer', '65', 'TID273'), (4, 'Prelim', 'SBI-JS202', 'Igbo', '70', 'TID015'), (5, 'Prelim', 'SBI-JS645', 'Igbo', '65', 'TID015');
我需要有关此脚本的帮助,该脚本可以很好地对sql数据库条目进行排序。但是我需要php代码来根据其得分(第一,第二,第三,第三,第五)给出位置。
<table border = '2'> <tr> <td>Student ID</td> <td>Score</td> <td>Position</td> </tr> <?php $con = mysql_connect("localhost", "root", ""); if(!$con) { echo "Could not establish connection to the database " . mysql_error(); } $mydb = mysql_select_db("sbicenugu", $con); if (!$mydb) { echo "Error selecting database " . mysql_error(); } $mystatement = mysql_query("SELECT * FROM jss1_prelim ORDER BY score DESC"); $dbcount = mysql_num_rows($mystatement); $selection = array(0 => ""); $nameselection = array(0 => ""); $i = 0; while ($row = mysql_fetch_array($mystatement)) { $selection[$i] = $row['score']; $nameselection[$i] = $row['idnumber']; $i++; } arsort($selection); ksort($nameselection); for ($x = 0; $x < $dbcount; $x++) { echo "<tr> <td>$nameselection[$x]</td> <td>$selection[$x]</td> <td>"; $grades = array($selection[$x]); $occurrences = array_count_values($grades); $grades = array_unique($grades); foreach($grades as $grade) { if($grade == end($grades))$i += $occurrences[$grade]-1; echo str_repeat('-'.($i + 1).'<br>',$occurrences[$grade]); $i+= $occurrences[$grade]; } echo"</td> </tr>"; } ?> </table>
这个剧本没有给我我对学生在课堂上的地位的期望。对于那些得分一样的人,它也给出了不同的价值位置。为此,我需要帮助。
此SQL代码将使您的排名列为“排名”,并且可以与部分结果很好地配合,因此即使显示多页的第二页,也始终具有正确的排名:
SELECT (SELECT COUNT(*) + 1 FROM jss1_prelim ref WHERE ref.score > main.score) as rank, id, term, idnumber, subject, score, teacher FROM jss1_prelim main ORDER BY score DESC
确保增加索引以score获得更好的性能。