MySQL 的“ORDER BY”在多重集上是否不同?
假设一个表有一列存储整数。
-----------------------------
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,如果 order by 子句中存在重复项,则无法保证重复项的显示顺序。
如果这是一个问题,您可以修改顺序以包含主键(我假设是 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).
Since id is a primary key, it should also be indexed. Thus the performance difference will be minimal.
执行此排序并不总是需要给您相同的结果顺序。订单不同的情况并不常见,但假设订单始终相同并不是 100% 安全,因为它不是您订购时所依据的唯一值。为了实现这一点,您还应该在初始订单之后包含主键。
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.
使用这个:
所以它将对“some_int”进行排序,然后使用“id”,“some_id”的重复项将使用“id”列设置为固定位置
Use this:
So it will sort for 'some_int' and then using 'id' Duplicates for 'some_id' will be set to fixed position using 'id' column