内容摘要:这是一家从事程序外包工作的外企招聘后台开发或与后台开发相关的SQL Server高级程序员的试题。
内容正文:
Question: How can I list non-contignous data?
In database pubs, I create a table test using statement as below, and I insert several row as below
程序代码
执行语句
Now I want to list the result of the non-contignous row as below,how can I do it?
程序代码
Missing after Missing before
------------- --------------
6 8
9 11
…
Answer 1:
select id from test t where not exists(select 1 from test where id=t.id+1)
or not exists(select 1 from test where id=t.id-1)
本人理解:找出不连续的记录。
记录的ID是自动增加的,后来又删除了记录,导致ID不连续。有时候为了方便查看哪些记录删除了。
本人解答
这种情况下也可以这样获得行号,不过效率就不敢恭维了。