Jump to content

How to select row with highest auto increment value?


python72

Recommended Posts

I have table with auto increment value and I would like to select data from that table only for the row with the highest auto increment value. Right now I use mysql_num_rows() but if for some reason one of the rows in the middle was removed the number of records will not be equal the value of the highest number and so far I was not succesfull doing it any other way, I have tried using max() but I can not get it to work, here is  the code I have tried:

 

$UpdateStartStopQuery = mysql_query("SELECT * FROM UpdateStartStop WHERE max(ID)");

Link to comment
Share on other sites

or if its good for you, can query the auto increment value for a table:

SHOW TABLE STATUS LIKE 'table_name';

heres the Auto_increment column.

 

That query would allow you to get the next auto-increment value (among other data about the table) - not the current highest auto-increment value. The OP stated he wanted the data for the record with the highest ID value. Determining the next auto-increment value would not help in that situation.

 

Futher, determining the next auto-increment value does not necessarily allow you to determine the ID of the highest value for that field in the table. The auto-increment values do not take into consideration any previous records that were deleted. So, if the last record created used the ID 20 and that record was deleted; then the next "Auto_increment" value for the table would be 21, but the record with the highest ID would be 19 - not 20 (since it was deleted)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.