0

我有如下的我的 Sql 数据库表

 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

现在我将为用户提供一个表单并告诉他们输入 ID 号并提交然后输出应该是。

如果用户输入 idno : 3 并提交表单,那么输出应该是

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

我在这里使用这段代码

   $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>";

在这里我无法计算排名并打印排名,我该怎么做?

基于总分,即 SUM(分数)作为技术排名应计算和打印

4

4 回答 4

1

在您的问题上投入了一点时间之后,我终于创建并测试了以下 SQL 查询,它产生了与您要求的结果相同的结果,并且它也很好地处理了关系。

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'

希望这可以帮助。

于 2013-12-22T15:03:27.820 回答
0

尝试这个:

   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
于 2012-10-09T12:30:53.830 回答
0
SELECT idno, SUM(score) AS tech
       FROM jbit 
       WHERE htno='$id'"
       GROUP BY idno
于 2012-10-09T12:25:10.947 回答
0

我尝试使用此代码...可能会帮助其他人我从用户那里获得结果并为用户个人资料图片上传表格并加入它,而不是在我计算用户积分并对其进行排序之后。最后检查并添加 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%'
于 2014-08-07T06:12:22.880 回答