Re: First and Last ID of a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



SELECT MIN(id), MAX(id) FROM mytable

:)

Hope that helps!

-TG

= = = Original message = = =

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


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux