Reputation: 9
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
Upvotes: 0
Views: 1701
Reputation: 1680
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%'
Upvotes: 0
Reputation: 137
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.
Upvotes: 1
Reputation: 79919
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
Upvotes: 0
Reputation: 4007
SELECT idno, SUM(score) AS tech
FROM jbit
WHERE htno='$id'"
GROUP BY idno
Upvotes: 0