简体   繁体   English

在MySQL中插入multplie行时的行顺序

[英]row order when inserting multplie rows in MySQL

In PHP, I create and execute SQL queries like the following. 在PHP中,我创建并执行如下所示的SQL查询。

INSERT INTO Table (Column1, Column2, Column3) VALUES
('1', '2', '3'),
('A', 'B', 'C'),
('AA', 'BB', 'CC');

However, the order in which they are inserted into the database is different every time I try this. 但是,每次尝试时,它们插入数据库的顺序都不同。 Is there a way that I can ensure that they are inserted in the order they are listed? 有没有办法可以确保按照列出的顺序插入它们?

Addition: Thanks guys for the help. 另外:谢谢大家的帮助。 I have using PHP to create MySQL tables from CSV files for a while. 我已经使用PHP从CSV文件创建MySQL表一段时间了。 In the past, I have always used created a table and inserted all the rows all at once. 在过去,我总是使用创建的表并一次性插入所有行。 In these case, the SQL table always had the same order as my INSERT query. 在这些情况下,SQL表始终与我的INSERT查询具有相同的顺序。 However, now I am creating a MySQL and then adding contents gradually. 但是,现在我正在创建一个MySQL,然后逐渐添加内容。 This is when the database order becomes random. 这是数据库顺序随机变化的时候。

I have overcome this by using ALTER TABLE ... ORDER BY queries, but I am curious as there was order in the first case and now it seems very random. 我已经通过使用ALTER TABLE ... ORDER BY查询克服了这个问题,但我很好奇,因为在第一种情况下有顺序,现在它似乎非常随机。

The default order is what the order of insert statements executed. 默认顺序是执行insert语句的顺序。 Unless there's a hierarchical relationship between the rows, the inserted order is irrelevant. 除非行之间存在层次关系,否则插入的顺序无关紧要。 If you want the output in a consistent fashion, you must define an ORDER BY clause in your query. 如果希望以一致的方式输出,则必须在查询中定义ORDER BY子句。

如果要按特定顺序检索行,唯一可以确定的方法是使用ORDER BY子句。

The order they're inserted isn't that important since you will likely be using the ORDER BY clause in your queries when pulling the data back out. 它们的插入顺序并不重要,因为在将数据拉回时,您可能会在查询中使用ORDER BY子句。 Remember, MySQL is for storing the data, not necessarily presenting it. 请记住,MySQL用于存储数据,不一定要呈现数据。

SELECT col1, col2
FROM table1
ORDER BY col2 ASC // fixes any ordering issues in the native-storage

A relation never defines order, so you cannot depend on that. 关系永远不会定义顺序,所以你不能依赖它。 You can use an ORDER BY clause to get the desired results. 您可以使用ORDER BY子句来获得所需的结果。

There is a way you can guarantee the order in which they're inserted: 有一种方法可以保证它们的插入顺序:

INSERT INTO Table (Column1, Column2, Column3) VALUES ('1', '2', '3');
INSERT INTO Table (Column1, Column2, Column3) VALUES ('A', 'B', 'C');
INSERT INTO Table (Column1, Column2, Column3) VALUES ('AA', 'BB', 'CC');

:-) :-)

But you need to understand that SQL is a relational algebra, working with result sets, and that the order in which things are inserted does not matter. 但是你需要理解SQL是一个关系代数,使用结果集,并且插入事物的顺序无关紧要。 Unless one of the columns is a time stamp of some sort or you have a auto-increment field that you want set in a specific order for the three rows, it won't matter (and both those are actually not good reasons for wanting order controlled). 除非其中一列是某种时间戳,或者你有一个自动增量字段要按三个特定顺序设置,否则无关紧要(这两者实际上并不是想要订单的好理由)受控)。

You're not guaranteed any particular when you extract the rows unless you specify what order you want them in so you shouldn't be concerned about the order they're created. 除非您指定所需的顺序,否则在提取行时不能保证任何特定内容,因此您不应该关注它们的创建顺序。 Without an order by clause, the order can actually change each time you do a select . 如果没有order by子句,每次执行select时,订单实际上都会更改。

Even though the order of insert is not guarantee by the DBMS it is typically FIFO. 即使DBMS不保证插入顺序,它通常也是FIFO。 First In First out. 先进先出。

There is no way (that I am aware of) to force the INSERT statement to occur in a specific Order. 没有办法(我知道)强制INSERT语句在特定的Order中发生。 Just doing a bare select, such as: 只做一个裸选,例如:

SELECT * FROM Table

Won't always return the data in the same order either. 也不会总是以相同的顺序返回数据。 This is because MySQL (and all other database systems) are meant for storing data, and it is up to the developer to query it appropriately. 这是因为MySQL(和所有其他数据库系统)用于存储数据,开发人员可以适当地查询它。 Luckily, there are many different ways to order, limit, and select the data, such as: 幸运的是,有许多不同的方式来订购,限制和选择数据,例如:

ORDER BY column_name ASC
LIMIT 10
WHERE column_name = "test"
WHERE column_name IN (1, 2, 3)
--etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM