gpt4 book ai didi

mysql - MySQL 5.1 和 5.6 中的 INSERT 和 SELECT 以不同的顺序返回

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:26 25 4
gpt4 key购买 nike

我正在尝试将 2 个插入到不同服务器上的两个数据库中。一个运行 MySQL 5.1(在 NAS 上),另一个运行 MySQL 5.6(在 Ubuntu 上)。

我做一个简单的

insert into table(pri1,pri2,value) values (1,1,1);
insert into table(pri1,pri2,value) values (1,2,1);

按照这个顺序。

当我做一个

select * from table;

MySQL 5.6 返回

pri1 pri2 value
1 1 1
1 2 1

这是正确的,因为这是它们插入的顺序。

当我做同样的事情时(以相同的顺序插入然后选择)MySQL 5.1 返回:

pri1 pri2 value
1 2 1
1 1 1

基本上它以相反的顺序存储数据,然后按该顺序显示。为什么会发生这种情况,我该如何防止它才能像在 MySQL 5.6 中一样正确显示数据?

谢谢

最佳答案

如果您需要有保证的特定订单,您必须使用 ORDER BY 子句,即

SELECT * FROM your_table ORDER BY your_column DESC

如果没有 ORDER BY 子句,有时结果可能看起来与您插入它们的顺序相同。但情况并非总是如此。顺序可能会受到索引或缓存等因素的影响。

By default, the rows in the result set produced by a SELECT statement are returned by the server to the client in no particular order. When a query is issued, the server is free to return the rows in any convenient order. This order can be affected by factors such as the order in which the rows are actually stored in the table, or which indexes are used to preocess the query. If it is neccessary for the output rows to be returned in a specific order, iclude an Order By clause that indicates how to sort the results.

来源:“面向开发人员的 MySQL(4.2.5 在 SELECT 语句中使用 Order BY)- Oracle 2011”

关于mysql - MySQL 5.1 和 5.6 中的 INSERT 和 SELECT 以不同的顺序返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23262824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com