For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > July 2007 > First and Last ID of a table









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author First and Last ID of a table
Kevin Murphy

2007-07-11, 6:58 pm

I have a table where I need to figure out the very first ID and the
very last ID, so here is what I wrote:

$first_query = "SELECT id FROM mytable ORDER BY id LIMIT 1";
$first_result = mysql_query($first_query,$con);
$first_id = mysql_result($first_result,0,'id');

$last_query = "SELECT id FROM mytable ORDER BY id DESC LIMIT 1";
$last_result = mysql_query($last_query,$con);
$last_id = mysql_result($last_result,0,'id');

I'm just wondering if there was any way to do this more efficiently,
like with one query instead of two. Or is this about as simple as I
can do it?

Thanks.



--
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com