Re: Auto-increment questions...
| From: | Andrew Kreps | Date: | Tue, 05 Oct 2004 21:18:55 +0000 |
| Subject: | Re: Auto-increment questions... | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-36633@lists.php.net to get a copy of this message | ||
On Tue, 5 Oct 2004 12:59:55 -0500, NIPP, SCOTT V (SBCSI) <sn4265@sbc.com> wrote:
> OK. The problem is I don't want the next "highest" number.
> There are gaps in the UID sequence. I need to find the next UNUSED
> number in the sequence which is rarely the "highest" number.
As far as I know, you'll have to write a little function to do that
for you. I don't know of a built-in database function that does what
you require. Assuming your application is PHP, Something such as this
might work (pardon the shorthand):
function getNextMissing ()
{
$result = "select id from the_table order by id";
while (list ($id) = mysql_fetch....)
{
if (isset ($lastId) && ($lastId < $id - 1))
{
$returnVal = $lastId + 1;
}
$lastId = $id;
}
return $returnVal;
}
You'll need to modify the function to handle there being no gaps in
the sequence (i.e. return $id + 1 if you never hit the logic in the
if), or for that matter, there being no id's. Otherwise, I think it's
a good starting point. Good luck!