a> 演示Here is SQLFiddle demo现在是php代码$link = mysql_connect('localhost', 'user', 'password');if (!$link) { die('Could not connect: ' . mysql_error());}$db_selected = mysql_select_db('dbname', $link);if (!$db_selected) { die ('Can\'t use db : ' . mysql_error());}$sql = " SELECT regd, Roll_no, Name_of_Student, Test_date, English, f_eng, Mizo, f_mz, Hindi, f_hn, Mathematics, f_maths, SS, f_ss, Science, f_sc, score, fmscore, perc, Rank FROM ( SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score FROM ( SELECT regd, Roll_no, Name_of_Student, Test_date, SUM(IF(Subject = 'English' , Mark_score, 0)) English, SUM(IF(Subject = 'English' , Full_mark, 0)) f_eng, SUM(IF(Subject = 'Mizo' , Mark_score, 0)) Mizo, SUM(IF(Subject = 'Mizo' , Full_mark, 0)) f_mz, SUM(IF(Subject = 'Hindi' , Mark_score, 0)) Hindi, SUM(IF(Subject = 'Hindi' , Full_mark, 0)) f_hn, SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, SUM(IF(Subject = 'Mathematics', Full_mark, 0)) f_maths, SUM(IF(Subject = 'SS' , Mark_score, 0)) SS, SUM(IF(Subject = 'SS' , Full_mark, 0)) f_ss, SUM(IF(Subject = 'Science' , Mark_score, 0)) Science, SUM(IF(Subject = 'Science' , Full_mark, 0)) f_sc, SUM(Full_mark) fmscore, SUM(Mark_score) score, SUM(Mark_score) / SUM(Full_mark) * 100 perc FROM cxexam, (SELECT @n := 0, @p := 0) n WHERE Test_date BETWEEN '2013-07-01' AND '2013-07-31' GROUP BY regd ORDER BY score DESC ) t ) r";$result = mysql_query($sql);if(!$result) { die(mysql_error()); // TODO: better error handling}while($row = mysql_fetch_assoc($result)) { echo "{$row['regd']} - {$row['Rank']}<br>";}输出(如预期):40 - 12 - 23 - 220 - 3 这篇关于任何替代解决方案,使这项工作? Php代码不工作,但mysql代码是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 08:33
查看更多