简体   繁体   中英

SQL ALTER TABLE Excluding a column

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.

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM