Re: Auto-increment questions...

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

 



On Tue, 5 Oct 2004 12:59:55 -0500, NIPP, SCOTT V (SBCSI) <sn4265@xxxxxxx> 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!

-- 
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