SQL 简单 ORDER BY

发布于 2024-10-09 07:12:00 字数 316 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冰魂雪魄 2024-10-16 07:12:00

通常我们在从表中获取数据时使用 order by

所以我们使用

SELECT * FROM `HSTable` ORDER BY `score` DESC

generally we use order by while fetching data from table

so we use

SELECT * FROM `HSTable` ORDER BY `score` DESC
心凉怎暖 2024-10-16 07:12:00

使用 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.

白色秋天 2024-10-16 07:12:00

您可以使用 SELECT 子句获取排序结果:

SELECT * FROM `HSTable` ORDER BY `score` DESC

You can get ordered results using SELECT clause:

SELECT * FROM `HSTable` ORDER BY `score` DESC
浪荡不羁 2024-10-16 07:12:00

我认为数据库表中数据的顺序并不重要,因为当您查询数据时,您可以按照您想要的方式对其进行排序。您希望将数据以正确的顺序存储在数据库中的原因是什么?

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?

若水微香 2024-10-16 07:12:00

这不是在 SQL 中选择记录的正确方法。使用这个代替:

SELECT * FROM `tablename` ORDER BY `id` DESC

That is not the correct way to select records in SQL. Use this instead:

SELECT * FROM `tablename` ORDER BY `id` DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文