php Mysql获取特定表的最后一个ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14255373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql get last id of specific table
提问by simon
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 :)
问候,西蒙 :)
采纳答案by Saharsh Shah
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);
回答by x4rf41
Is this what you are looking for?
这是你想要的?
SELECT id FROM blahblah ORDER BY id DESC LIMIT 1
回答by flu
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。
回答by Vishnu R
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.
但是,如果您使用的是流量大的网站,则可能会出现准确结果。
回答by Bhavik Shah
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 中使用它
回答by Nazir Ali
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');

