Is the order of inserts specified for INSERT IGNORE … SELECT?

旧巷老猫 提交于 2021-02-08 09:48:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!