MySQL:插入具有相同AI值的多行 [英] MySQL: Insert multiple rows with same AI value

查看:83
本文介绍了MySQL:插入具有相同AI值的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这是我的桌子:

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');

如何确保他们在单个查询中都收到相同的自动递增ID?

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

推荐答案

您需要将第一个AI值保留在变量中,并在INSERT查询中将其传递给不同的对

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

这篇关于MySQL:插入具有相同AI值的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆