在MySQL中插入多行时的行顺序 [英] row order when inserting multplie rows in MySQL
问题描述
在PHP中,我创建并执行如下所示的SQL查询.
In PHP, I create and execute SQL queries like the following.
INSERT INTO Table (Column1, Column2, Column3) VALUES
('1', '2', '3'),
('A', 'B', 'C'),
('AA', 'BB', 'CC');
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?
添加: 谢谢大家的帮助.我有一段时间使用PHP从CSV文件创建MySQL表.过去,我一直使用创建表并一次插入所有行的方式.在这种情况下,SQL表始终与我的INSERT查询具有相同的顺序.但是,现在我要创建一个MySQL,然后逐渐添加内容.这是数据库顺序变得随机的时候.
Addition: Thanks guys for the help. I have using PHP to create MySQL tables from CSV files for a while. 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. However, now I am creating a MySQL and then adding contents gradually. This is when the database order becomes random.
我已经通过使用ALTER TABLE ... ORDER BY查询克服了这个问题,但是我很好奇,因为在第一种情况下有顺序,现在看来非常随机.
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.
推荐答案
默认顺序是执行插入语句的顺序.除非各行之间存在层次关系,否则插入顺序无关紧要.如果要以一致的方式输出,请必须在查询中定义ORDER BY
子句.
The default order is what the order of insert statements executed. 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.
这篇关于在MySQL中插入多行时的行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!