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)");

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.