Re: First and Last ID of a table
| From: | tg-php at gryffyndevelopment dot com | Date: | Wed, 11 Jul 2007 20:13:54 +0000 |
| Subject: | Re: First and Last ID of a table | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-43770@lists.php.net to get a copy of this message | ||
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.