Question

I have a MySQL table named 'leaderboard' and it has 5 columns 'name','regno','dept','gpa','rank' I have set rank to AUTO_INCREMENT and now I want to order the table by 'gpa', but rank of the row should not change. The table should be sorted permanently not just printing output.

For example:-

regno name dept gpa rank                 
1      a    c    8   1        
2      b    d    9   2

After sorting the table should be like this:-

regno name dept gpa rank   
2      b    d    9   1   
1      a    c    8   2

See the rank column is not changed.

Was it helpful?

Solution 2

Rather than storing the Rank in your table and selecting it out try to calculate it on the fly. Try something like the following:

SELECT    regno, 
          name, 
          dept, 
          gpa
          @curRank := @curRank + 1 AS rank
FROM      leaderboard, (SELECT @curRank := 0) r
ORDER BY  gpa;

OTHER TIPS

I don't think you can get that or you can get that!
Because what you see is a logical representation of you bytes stored in a database, so it's all a representation!
What you can do is create a view for you with a specific sort order on that gpa column ! What do you think about that ?

Just to make a point : - i am copying @SARIN

create view Oreder_data_like_i_want_to_see_it 
 as 
SELECT    regno, 
          name, 
          dept, 
          gpa
          @curRank := @curRank + 1 AS rank
FROM      leaderboard, (SELECT @curRank := 0) r
ORDER BY  gpa;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top