SQL 简单 ORDER BY
我是 SQL 新手,我认为这个问题应该不难回答。我有一个游戏的高分表,其中包含名称、得分和排名列。我想知道每次添加新分数时如何按降序对表格进行排序,以便表格始终保持按分数排序。
我知道这是错误的做法,但我希望这能让我的观点更加清晰。
UPDATE `HSTable`.`Highscores` ORDER BY `Highscores`.`score` DESC;
解决这个问题的正确方法是什么?
还有一件事,有没有一种方法可以设置它,以便排名值始终保持在 SQL 假定的位置,例如,无论分数如何,第一名始终位于顶部?
I'm new to SQL and I don't think this question should be hard to answer. I have a high-score table for a game that contains the columns name, score, and rank. I want to know how I can order the table by descending order each time a new score is added so the table can always stay ordered by score.
I know this is the wrong way of doing this, but I hope this makes my point kind of clearer.
UPDATE `HSTable`.`Highscores` ORDER BY `Highscores`.`score` DESC;
What is the correct way of approaching this?
One more thing, is there a way I can set it so that the ranking value always stays where it's suppose to be from the SQL, for example, 1st place is always at the top regardless the score?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常我们在从表中获取数据时使用 order by
所以我们使用
generally we use order by while fetching data from table
so we use
使用 RANK 或 DENSE_RANK 等排名函数对结果进行排名。为了获得更好的性能,您可以添加以分数降序开头的聚集索引。
Use a ranking function like RANK or DENSE_RANK to rank the results. For better performance you can add a clustered index that starts with score descending.
您可以使用
SELECT
子句获取排序结果:You can get ordered results using
SELECT
clause:我认为数据库表中数据的顺序并不重要,因为当您查询数据时,您可以按照您想要的方式对其进行排序。您希望将数据以正确的顺序存储在数据库中的原因是什么?
I don't believe the order of the data in your database table should really matter because when you query the data you can sort it any which way you would like. What is the reason why you want to have the data stored in the database in the correct order?
这不是在 SQL 中选择记录的正确方法。使用这个代替:
That is not the correct way to select records in SQL. Use this instead: