mysql获取相同的auto

mysql获取相同的auto

本文介绍了mysql获取相同的auto_increment值到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的答案.我做了一段时间的数据库工作.我试图从插入另一个表的值的一个表中获取auto_increment值.有一个简单的方法可以做到这一点.例如,我做了:

This may have a really easy answer. I have done much database stuff for a while. I am trying to get the auto_increment value from one table inserted into the value on another table. is there an easy way of doing this. For eg i have done:

CREATE TABLE table_a (
id int NOT NULL AUTO_INCREMENT,
a_value  varchar(4),
PRIMARY KEY (id)
);

CREATE TABLE table_b (
id int NOT NULL,
b_value varchar(15),
FOREIGN KEY (id) REFERENCES table_a (id)
);

现在我想将值插入表中,但我希望table_a和table_b的'id'值相同.到目前为止,我有:

Now i want to insert values into the table but I would like 'id' values for table_a and table_b to be the same. So far i have:

INSERT INTO table_a VALUES (NULL, 'foobar');

但是我不知道如何从table_a提取auto_incermented'id'数到table_b的'id'值中.我已经看过SELECT @id = LAST_INSERT_ID()了,但无法正常工作.

But I do not know how to go about extracting the auto_incermented 'id' number from table_a into the 'id' value of table_b. I have looked at SELECT @id = LAST_INSERT_ID() but can not get it to work.

推荐答案

LAST_INSERT_ID(),并且不需要select语句部分.

LAST_INSERT_ID() and no need for the select statement part.

这篇关于mysql获取相同的auto_increment值到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:55