vikela Posted May 8, 2009 Share Posted May 8, 2009 i have a table that will have an auto increment field,and i would like to retrieve the record with the biggest number and show the record.how can i be able to do the sql statement because i have tried with the limit but no joy. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/ Share on other sites More sharing options...
RichardRotterdam Posted May 8, 2009 Share Posted May 8, 2009 mysql has a max() function. You could use that Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829316 Share on other sites More sharing options...
AmandaF Posted May 8, 2009 Share Posted May 8, 2009 What are you getting when you try using LIMIT? SELECT ID FROM table ORDER BY ID DESC LIMIT 1 would have been my solution. Is that what you tried? Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829439 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 AmandaF, it's uncertain which column the user wants the MAX of. It may not be ID, but your solution is a good one if that is the case. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829445 Share on other sites More sharing options...
fenway Posted May 8, 2009 Share Posted May 8, 2009 Don't rely on the ID - not necessarily the most "recent' if anything's been updated. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829485 Share on other sites More sharing options...
AmandaF Posted May 8, 2009 Share Posted May 8, 2009 AmandaF, it's uncertain which column the user wants the MAX of. It may not be ID, but your solution is a good one if that is the case. If vikela doesn't want the ID, then SELECT ID, colum_that_vikela_wants_the_max_of FROM table ORDER BY column_that_vikela_wants_the_max_of LIMIT 1 Of course, if the column isn't unique and more than one row has the max value, then only one of those rows will get returned. In that case, it would be better to use the max() function. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829506 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 AmandaF: 1. No need to select ID. 2. You forgot DESC in your ORDER BY clause. Let's just wait for vikela to reply. Let's not guess. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829579 Share on other sites More sharing options...
AmandaF Posted May 8, 2009 Share Posted May 8, 2009 AmandaF: 1. No need to select ID. 2. You forgot DESC in your ORDER BY clause. Let's just wait for vikela to reply. Let's not guess. 1) I thought vikela would need to be able to identify which row the result belonged to. 2) Thanks for catching that for me. Quote Link to comment https://forums.phpfreaks.com/topic/157336-showing-the-biggest-number/#findComment-829673 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.