Mysql得到特定表的最后id。

时间:2022-09-25 16:17:54

I have to get last insert id from a specific inserted table?. Lets say i have this code:

我必须从一个特定的插入表中获取最后的插入id ?假设我有这个代码:

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah2 (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah3 (test1, test 2, lastid) VALUES ('test1', 'test2', last id of blahblah);

How do i get the insert id of table blahblah in table blahblah3? LAST_INSERT_ID() only gives you the last insert id

如何在blahblah3中获取表blahblah的插入id ?LAST_INSERT_ID()只提供最后一个插入id

Regards, Simon :)

西蒙:问候)

6 个解决方案

#1


15  

You can use LAST_INSERT_ID() function. Try this:

您可以使用LAST_INSERT_ID()函数。试试这个:

INSERT INTO blahblah (test1, test2) VALUES ('test1', 'test2');

SELECT LAST_INSERT_ID() INTO @blahblah;

INSERT INTO blahblah2 (test1, test2) VALUES ('test1', 'test2');

INSERT INTO blahblah3 (test1, test2, lastid) VALUES ('test1', 'test2', @blahblah);

#2


12  

Is this what you are looking for?

这就是你要找的吗?

SELECT id FROM blahblah ORDER BY id DESC LIMIT 1

#3


8  

If you want to do it in a single statement use:

如果您想在一个语句中完成,请使用:

INSERT INTO blahblah3 (test1, test2, lastid)
VALUES ('test1', 'test2', (select MAX(id) FROM blahblah));

This way you don't need to save any variables beforehand which assures you'll get the latest ID at that exact moment.

这样,您就不需要预先保存任何变量,这将确保您在该时刻获得最新的ID。

#4


2  

You can use mysql_insert_id(); function to get a quick answer.

您可以使用mysql_insert_id();函数以获得快速的答案。

But if you are using a heavy traffic site, chances of in accurate results exist.

但是,如果你使用的是一个交通繁忙的站点,就有可能得到准确的结果。

#5


1  

You can use LAST_INSERT_ID() function.

可以使用LAST_INSERT_ID()函数。

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
    //this query will return id. Save it in one variable

 select LAST_INSERT_ID()

In short, save the last insert id in one variable and then use it in blahblah3

简而言之,将最后一个插入id保存在一个变量中,然后在blahblah3中使用它

#6


0  

you can also find last id by query in codeigniter As

您还可以在codeigniter As中通过查询找到最后一个id

$this->db->order_by('column name',"desc");
   $this->db->limit(1);
 $id=  $this->db->get('table name');

#1


15  

You can use LAST_INSERT_ID() function. Try this:

您可以使用LAST_INSERT_ID()函数。试试这个:

INSERT INTO blahblah (test1, test2) VALUES ('test1', 'test2');

SELECT LAST_INSERT_ID() INTO @blahblah;

INSERT INTO blahblah2 (test1, test2) VALUES ('test1', 'test2');

INSERT INTO blahblah3 (test1, test2, lastid) VALUES ('test1', 'test2', @blahblah);

#2


12  

Is this what you are looking for?

这就是你要找的吗?

SELECT id FROM blahblah ORDER BY id DESC LIMIT 1

#3


8  

If you want to do it in a single statement use:

如果您想在一个语句中完成,请使用:

INSERT INTO blahblah3 (test1, test2, lastid)
VALUES ('test1', 'test2', (select MAX(id) FROM blahblah));

This way you don't need to save any variables beforehand which assures you'll get the latest ID at that exact moment.

这样,您就不需要预先保存任何变量,这将确保您在该时刻获得最新的ID。

#4


2  

You can use mysql_insert_id(); function to get a quick answer.

您可以使用mysql_insert_id();函数以获得快速的答案。

But if you are using a heavy traffic site, chances of in accurate results exist.

但是,如果你使用的是一个交通繁忙的站点,就有可能得到准确的结果。

#5


1  

You can use LAST_INSERT_ID() function.

可以使用LAST_INSERT_ID()函数。

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
    //this query will return id. Save it in one variable

 select LAST_INSERT_ID()

In short, save the last insert id in one variable and then use it in blahblah3

简而言之,将最后一个插入id保存在一个变量中,然后在blahblah3中使用它

#6


0  

you can also find last id by query in codeigniter As

您还可以在codeigniter As中通过查询找到最后一个id

$this->db->order_by('column name',"desc");
   $this->db->limit(1);
 $id=  $this->db->get('table name');