python72 Posted January 18, 2011 Share Posted January 18, 2011 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)"); Quote Link to comment https://forums.phpfreaks.com/topic/224797-how-to-select-row-with-highest-auto-increment-value/ Share on other sites More sharing options...
Psycho Posted January 18, 2011 Share Posted January 18, 2011 << Moving topic to MySQL forum >> SELECT * FROM TABLE ORDER BY ID DESC LIMIT 1 Order the results from highest to lowest by ID then LIMIT the results to just the first record Quote Link to comment https://forums.phpfreaks.com/topic/224797-how-to-select-row-with-highest-auto-increment-value/#findComment-1161155 Share on other sites More sharing options...
bh Posted January 18, 2011 Share Posted January 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224797-how-to-select-row-with-highest-auto-increment-value/#findComment-1161220 Share on other sites More sharing options...
Psycho Posted January 18, 2011 Share Posted January 18, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/224797-how-to-select-row-with-highest-auto-increment-value/#findComment-1161462 Share on other sites More sharing options...
python72 Posted January 19, 2011 Author Share Posted January 19, 2011 Thanks a lot guys for taking your time to help. It works great. Quote Link to comment https://forums.phpfreaks.com/topic/224797-how-to-select-row-with-highest-auto-increment-value/#findComment-1161698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.