MySQL: Insert multiple rows with same AI value

为君一笑 提交于 2019-12-12 06:17:47

问题


Let's say this is my table:

CREATE TABLE tab (
    id INT AUTO_INCREMENT NOT NULL,
    val VARCHAR(9),
    KEY(id),
    PRIMARY KEY (xx)
);

Would it possible to insert multiple rows at the same time in a way that they would all get the same auto-increment value?

The following works, but increments each new row, regardless of the fact that we are doing a single query.

INSERT INTO tab (id,val) VALUES (LAST_INSERT_ID(),'a'), (LAST_INSERT_ID(),'b');

How could I make sure they all receive the same auto-incremented ID in a single query?


回答1:


You will need to keep the first AI value in a variable and pass it in the INSERT query for different pairs



来源:https://stackoverflow.com/questions/14257683/mysql-insert-multiple-rows-with-same-ai-value

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