MySQL Forums
Forum List  »  Newbie

Re: How to create a solid sequence?
Posted by: Rick James
Date: June 24, 2011 11:11PM

I think this will (somewhat efficiently) find the first hole in tbl. And if there are no holes, find the next value off the end.
SELECT MIN(id)
    FROM (
        SELECT  a.id
            FROM tbl a
            LEFT JOIN tbl b  ON a.id = b.id+1
            WHERE b.id IS NULL
         ) AS x

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.