0

以下のようなmysqlテーブルがあります:

CREATE TABLE IF NOT EXISTS `cxexam` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`regd` int(11) NOT NULL,
`Name_of_Student` varchar(100) COLLATE latin1_general_ci NOT NULL,
`Class` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Roll_no` int(11) NOT NULL,
`Section` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Name_of_exam` varchar(100) COLLATE latin1_general_ci NOT NULL,
`Test_date` date NOT NULL,
`Subject` varchar(50) COLLATE latin1_general_ci NOT NULL,
`Full_mark` int(11) NOT NULL,
`Mark_score` int(11) NOT NULL,
`Year` year(4) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7 ;

--

-- テーブルのデータをダンプしていますcxexam

INSERT INTO `cxexam` (`id`, `regd`, `Name_of_Student`, `Class`, `Roll_no`, `Section`, `Name_of_exam`, `Test_date`, `Subject`, `Full_mark`, `Mark_score`, `Year`) VALUES

(6, 20, 'Ramdina', 'X', 9, 'A', 'Second Term Unit Test', '2013-07-19', 'English', 20, 18, 2013),
(3, 2, 'Zonundanga', 'X', 5, 'A', 'Second Term Unit Test', '2013-07-19', 'English',   20, 12, 2013),
(4, 40, 'Lalnunkimi', 'X', 10, 'A', 'Second Term Unit Test', '2013-07-19', 'English', 20, 18, 2013);

mysql クエリは以下を生成します。

regd   totalscore    rank
20   18   1
2   18   1
40  12   2

regd='2'そしてphpを使っている人のランクを出力したいです。次の php コードと mysql コードを使用していますが、コードを機能させるにはページを更新する必要があります。クエリは phpmyadmin で正常に動作していますが、ほとんどの1場合、regd.

mysql_select_db($database_dbconnect, $dbconnect);
$query_myrank = "SELECT Distinct regd, Test_date, Year, Name_of_Student, TOTALSCORE,  Rank FROM 
(SELECT *, IF(@marks = (@marks := TOTALSCORE), @auto, @auto := @auto + 1) AS
Rank FROM (SELECT Name_of_Student, regd, Test_date, Year, SUM(Mark_score) AS TOTALSCORE 
FROM cxexam, (SELECT @auto := 0, @marks := 0) AS init GROUP BY regd, Year ORDER BY TOTALSCORE DESC) t) AS result HAVING regd='2' and Year='2013' and Test_date between '2013-07-01' and '2013-07-30'";
$myrank = mysql_query($query_myrank, $dbconnect) or die(mysql_error());
$row_myrank = mysql_fetch_assoc($myrank);
$Rank= $row_myrank['Rank'];

学校のマークシート管理システムでこのランキングを適用しようとしているので、誰かが PHP を使用<?php echo $row_myrank['Rank'];?>して代替ソリューションを入手できれば、非常に嬉しく思います。

これは私の新しいコードです:

<?php 
mysql_select_db($database_dbconnect, $dbconnect);
$query_myrank = "SELECT Distinct regd, Test_date, Year, Name_of_Student, TOTALSCORE, Rank 
FROM (SELECT *, IF(@marks = (@marks := TOTALSCORE), @auto, @auto := @auto + 1) AS Rank 
FROM (SELECT Name_of_Student, regd, Test_date, Year, SUM(Mark_score) AS TOTALSCORE 
FROM cxexam, (SELECT @auto := 0, @marks := 0) AS init GROUP BY regd, Year ORDER BY TOTALSCORE DESC) t) AS result HAVING Year='$yr' and Test_date between '$fdate' and '$tdate'";
$myrank = mysql_query($query_myrank, $dbconnect) or die(mysql_error());

$i = 0;
$j = 1;
$data = array();
while($row_myrank = mysql_fetch_assoc($myrank))
{
$data[$i] = $row_myrank;
if(isset($data[$i - 1]) && $data[$i - 1]['TOTALSCORE'] == $data[$i]['TOTALSCORE'])
{
   $data[$i]['Rank'] = $j;
}else{
   $data[$i]['Rank'] = ++$j;
}
   $i++;
}
foreach($data as $key => $value)
{
if($value['regd'] == $regd)
{
echo $value['Rank'];
}
}
?>
4

1 に答える 1