MySQL 的“ORDER BY”在多重集上是否不同?

发布于 2024-12-23 22:15:16 字数 426 浏览 0 评论 0原文

假设一个表有一列存储整数。

-----------------------------
id    |  some_int  | some_other_value
-----------------------------
1        5           hello
2        9           how
3        987         are
4        5           you
5        9           thanks
6        1           for
7        5           answering. :-)

SELECT * FROM mytable ORDER BY some_int; 是否不同?这意味着每次查询后它总是以相同的顺序返回行?

Assuming a table with a column where integers are stored.

-----------------------------
id    |  some_int  | some_other_value
-----------------------------
1        5           hello
2        9           how
3        987         are
4        5           you
5        9           thanks
6        1           for
7        5           answering. :-)

Is SELECT * FROM mytable ORDER BY some_int; distinct? Meaning will it always return the rows in the same order, after each query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

对你的占有欲 2024-12-30 22:15:16

据我所知,如果 order by 子句中存在重复项,则无法保证重复项的显示顺序。

如果这是一个问题,您可以修改顺序以包含主键(我假设是 id)。

ORDER BY some_int, id

由于 id 是主键,因此也应该建立索引。因此,性能差异将是最小的。

To the best of my knowledge if there are duplicates in the order by clause, there is no guarantee for the order in which the duplicates are presented.

If this is a concern, you could modify the order by to include the primary key (id I am assuming).

ORDER BY some_int, id

Since id is a primary key, it should also be indexed. Thus the performance difference will be minimal.

筱武穆 2024-12-30 22:15:16

执行此排序并不总是需要给您相同的结果顺序。订单不同的情况并不常见,但假设订单始终相同并不是 100% 安全,因为它不是您订购时所依据的唯一值。为了实现这一点,您还应该在初始订单之后包含主键。

SELECT * FROM mytable ORDER BY some_int, id

Doing this order by does not always have to give you the same result order. It is not frequent that the order will be different but it's not a 100% safe to assume that the order will always be the same as it is not an unique value that you're ordering by. To achieve this you should also include the primary key after the initial order.

SELECT * FROM mytable ORDER BY some_int, id
鹿港小镇 2024-12-30 22:15:16

使用这个:

SELECT * FROM mytable ORDER BY some_int,id;

所以它将对“some_int”进行排序,然后使用“id”,“some_id”的重复项将使用“id”列设置为固定位置

Use this:

SELECT * FROM mytable ORDER BY some_int,id;

So it will sort for 'some_int' and then using 'id' Duplicates for 'some_id' will be set to fixed position using 'id' column

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