Print Rank from sum in PHP & MySql

这一生的挚爱 提交于 2019-12-13 03:16:24

问题


I have My Sql Database Table as below

 idno      Name        Subject         Score 

  1        Mahesh      English           55
  1        Mahesh      Maths             25
  1        Mahesh      Science           35
  2        Richards    English           65 
  2
  2 
  3
  3         
  3
  .................. Like ways so on till id number 12000

Now i will provide a form for the user and tell them to enter id number and submit then the output should be.

If User Enters idno : 3 and submit the form then the output should be

   IDNO        NAME         TOTAL SCORE       RANK
     1         MAHESH           95            2546 (Example)

and here i am using this code

   $id = mysql_real_escape_string($_POST['id']);
   $sum = "SELECT idno, SUM(score) AS tech
   FROM jbit 
   WHERE htno='$id'";
   $result1 = mysql_query($sum);
   echo "
   <center><table id='mytable' cellspacing='0'  border=3 align=center>
   <tr>
   <TH scope='col'>IDNO</TH>
   <TH scope='col'>NAME</TH>
   <TH scope='col'>TOTAL SCORE</TH>
   <TH scope='col'>RANK</TH>
   </tr><center>";
   while ($row = mysql_fetch_assoc($result1)){
echo "<tr>";
   echo "<td align=center>" . $row['idno']. "</td>";
   echo "<td align=center>" . $row['name']. "</td>";
   echo "<td align=center>" . $row['tech']. "</td>";
   echo "</tr>";

Here I am unable to calculate the rank and print the rank, how can I do this?

Based on Total Score i.e. SUM(Score) as Tech Rank shold be calculated & Printed


回答1:


After investing a little bit of time on your issue, I have finally created and tested the following SQL query, it produces the same result as you requested and it also handles the ties very well.

SELECT idno, name, rank,total_score
FROM (SELECT *,  IF(@marks=(@marks:=total_score), @auto, @auto:=@auto+1) AS rank 
FROM (SELECT * FROM 
  (SELECT idno, name, SUM(score) AS total_score 
    FROM jbit, 
    (SELECT @auto:=0, @marks:=0) as init 
     GROUP BY name) sub ORDER BY total_score DESC)t) as result
WHERE idno ='1'

Hope this helps.




回答2:


SELECT idno, SUM(score) AS tech
       FROM jbit 
       WHERE htno='$id'"
       GROUP BY idno



回答3:


You can try like this:

Link

Please check above link hope this help you.




回答4:


Try this:

   SET @rownum = 0; 
   Select *,  (@rownum := @rownum+1) as RANK
   FROM
   (
      SELECT * 
      FROM
      (
         SELECT IDNO, NAME, SUM(score) AS TOTASCORE      
         FROM jbit 
         GROUP BY IDNO, NAME
       ) sub
       ORDER BY TOTASCORE DESC --The rank is calculated based on this field
   ) t
   WHERE IDNO = @IDNOParam



回答5:


I try to this code... may help other I am getting result from users and upload table for user profile pic and join it than after i am calculating user points and sorting on them. At last am checking and adding wo

set @row_num = 0;
set @calp =0;
select  if(@calp=(@calp:=user.cal_points), @row_num, @row_num := @row_num + 1) as row_number,user.* from 
(select user_skills.*,users.username,upload.file_name from user_skills join users on user_skills.user_id=users.id join upload on upload.upload_id=users.profile_pic order by user_skills.cal_points desc) as user
WHERE user.skill_name LIKE  '%ph%'






==========for both search =============
set @row_num = 0;
set @calp =0;

select temp.* from
(select  if(@calp=(@calp:=user.cal_points), @row_num, @row_num := @row_num + 1) as row_number,user.* from 
(select user_skills.*,users.username,upload.file_name from user_skills join users on user_skills.user_id=users.id join upload on upload.upload_id=users.profile_pic order by user_skills.cal_points desc) as user
WHERE user.skill_name LIKE  '%ph%') as temp
WHERE temp.username LIKE '%a%'


来源:https://stackoverflow.com/questions/12799949/print-rank-from-sum-in-php-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!