How to improve this php mysql code? -


with these codes echo rank of student regd equal $regd. in fact, working code. however, advised friend in mysql statement distinct , group by should not used together. newbie, not figure out how implement without using distinct because not return rows without distinct. can suggest me how improve these codes?

<?php  mysql_select_db($database_dbconnect, $dbconnect); $query_myrank = "select distinct regd, name_of_exam,                          name_of_student, totalscore, rank                   (select *, if(@marks = (@marks := totalscore),                                @auto, @auto := @auto + 1) rank                         (select name_of_student, regd,                                     name_of_exam, sum(mark_score) totalscore                               cixexam, (select @auto := 0,                                                     @marks := 0) init                                              group regd                               order totalscore desc) t) result                   having (name_of_exam='first terminal exam' or                           name_of_exam='first term test')";  $myrank = mysql_query($query_myrank, $dbconnect) or die(mysql_error());  $i = 0; $j = 0; $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 $key => $value) {     if($value['regd'] == $regd)     {         echo $value['rank'];     } } ?>     

distinct slower group by. can go without using group by , distinct , want achieve.

select regd, roll_no, name_of_student, name_of_exam,        totalscore, rank   (   select t.*, if(@p = totalscore, @n, @n := @n + 1) rank, @p := totalscore        (     select regd, roll_no, name_of_student, name_of_exam,              sum(mark_score) totalscore     cixexam, (select @n := 0, @p := 0) n       (name_of_exam='first terminal exam' or name_of_exam='first term test')      group regd       order totalscore desc   ) t ) r 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -