问题
I have a table like:
CREATE TABLE {
email VARCHAR PRIMARY,
last_login DATE
}
And I can populate with a select result set like:
("a@b.c", "2019-01-01"),
("a@b.c", "2019-02-01")
If I will insert into that table using INSERT IGNORE ... SELECT
, is it specified which row gets inserted and which is ignored?
Found specification:
- "Specify IGNORE to ignore rows that would cause duplicate-key violations." from https://dev.mysql.com/doc/refman/5.5/en/insert-select.html. But this does not say which is the duplicate and which is the not-duplicate.
回答1:
The insert
statement doesn't specify the order the rows are inserted - the select
statement does. However, unless you explicitly define the order with an order by
clause, the order select
returns rows is completely up to the database, and shouldn't be trusted.
In other words, if you care which row gets inserted and which gets ignored, add an order by
clause to your select
statement and make sure that the row you want to insert comes first.
来源:https://stackoverflow.com/questions/56841179/is-the-order-of-inserts-specified-for-insert-ignore-select