日期:2014-05-18  浏览次数:20588 次

急急急,求大侠救命
我在数据库中有no001,no003,no004,no005,no006......等不能连继的用户名,现在要求插入一个用户,是这些用户名中没有出现的最小的那位,如插入刚自动选择no002.那位大侠能给小弟一点儿思路.

------解决方案--------------------
假设表名customer,字段为id
select top 100 percent id from customer
where not exists (select top 100 percent id from customer a where replace(customer.id, 'no ', ' ')+1=replace(a.id, 'no ', ' ') order by id )
order by id
------解决方案--------------------
通过这个sql语句可以找到断点,插入的时候你把找到的断点后面的数字加一就可以了,如果表没有按照ID排序,要加上order by ,同时还要加上top