How to predict the next auto-increment number?
Moderator: General Moderators
How to predict the next auto-increment number?
My primary ID in a MySQL database table is set to auto-increment. My question is, how do I predict or know what the next number is ahead of time before the entry is submitted to the database? Is this possible to know? In most cases, I found that this number is sequential but other cases I found that this number also jumps or skips if for example, I have ID 1, 2, 3, 4, 5, 6 and then I deleted entry 6, the next number would be 7 and not 6.
Help is appreciated.
Help is appreciated.
http://dev.mysql.com/doc/refman/5.0/en/ ... tatus.html
However, why do you want to know the next autoincrement value?
[edit]
URL corrected
[/edit]
However, why do you want to know the next autoincrement value?
[edit]
URL corrected
[/edit]
Last edited by Weirdan on Tue Oct 18, 2005 10:25 am, edited 1 time in total.
You can also call:
But ya, why do you want to predict the next id?
Code: Select all
SELECT LAST_INSERT_ID() FROM `my_table`;Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
I think you did meanpickle wrote:You can also call:Code: Select all
SELECT LAST_INSERT_ID() FROM `my_table`;
Code: Select all
select last_insert_id();Ah, my bad. You're right ~Weirdan - using LAST_INSERT_ID() will only really work if one's only inserting into a single table with an auto_increment value. Otherwise, the values could be screwed up.
To get the next value, you can just add one:
To get the next value, you can just add one:
Code: Select all
SELECT LAST_INSERT_ID() + 1;Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Many thanks for all your help. The reason I want to know the id is in the following situation. I'm creating a website that its contents are store on a MySQL database tabel. So when the user click on a navigation menu, it has the following link characteristics:
<a href='found.php?id=3'>Welcome</a>
And so on. The id 3 is related to the primary id that is auto-increment. Now, for creating new pages, I need to know this id ahead of time so I can insert into the URL field on the table at the same the new page is inserted by the user.
<a href='found.php?id=3'>Welcome</a>
And so on. The id 3 is related to the primary id that is auto-increment. Now, for creating new pages, I need to know this id ahead of time so I can insert into the URL field on the table at the same the new page is inserted by the user.