select min(id)+1
from mytable t0 where not exists
    (select id from mytable t1 where t1.id=t0.id+1)

解决方案 »

  1.   

    select min(id+1) as newid from tablename a
    where not exists (
    select 1 from tablename where id=a.id+1
    )
      

  2.   

    如果你有需要的话,可以另建一个表tb,其中N字段的记录为
    1、2、3、4、5......10000(根据你的需要)
    在需要用的时候可以这样select min(b.N)
    from b
    where b not in(select id from 需要取得取小可用值的表)应该是最快的办法
      

  3.   

    先谢谢各位。select min()使我现在使用的方法,但是数据量大了之后速度有点慢。
    Chiff的方法可能会比较快,但是维护相关数据会大大增加系统的复杂度。希望还有更好的方法。