简体   繁体   English

即使删除该行也获得插入的最后一行ID

[英]Get last row id inserted even after deleting the row

I'm trying to get the last row id from a table in MySQL, using PHP and I have the following code: 我正在尝试使用PHP从MySQL的表中获取最后一行ID,我有以下代码:

$query = "SELECT MAX(id) FROM #__djcf_items";   
$db->setQuery($query);
$id = $db->loadResult()+1;

And it works, but if I delete last row then that code will return what I want -1... 它可以工作,但是如果我删除最后一行,那么该代码将返回我想要的-1 ...

How can I get the "global" last row id even after deleting row(s)? 即使删除行后,如何获取“全局”最后一行ID?

You can find solution on this topic. 您可以找到有关主题的解决方案。

SELECT TABLE_ROWS
FROM information_schema.tables 
WHERE table_name='the_table_you_want' -- Can end here if only 1 DB 
  AND table_schema = DATABASE();      -- See comment below if > 1 DB

您可以在数据库上运行另一个select,并向其查询数据库最后一行的最后一个id:

SELECT * FROM your_table ORDER BY id DESC LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM