找出ID不连续的记录,偶没思路了

wishton_126 2004-10-25 11:19:25

--测试数据

create table table_test
(
id int ,
name Varchar(50)
)

insert into table_test


select 1,'A'
union all

Select 2,'B'

union all

Select 3,'C'

union all

Select 11,'S'

union all

Select 12,'Q'

union all

Select 15,'W'

union all

Select 17,'Q'

union all

Select 19,'Z'

--Select * from table_test





输出结果为

id name
----------- --------
3 C
11 S
12 Q
15 W
17 Q
19 Z

11, 12 虽然连续, 但 3,11 不连续, 12,15 不连续, 所以 11,12 也要 输出
...全文
195 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsxaa 2004-10-26
  • 打赏
  • 举报
回复
这个也行的
select *
from table_test a
where (not exists(select 1 from table_test where id=a.id-1 )
and a.id<>(select min(id) from table_test) )
or ( not exists(select 1 from table_test where id=a.id+1 )
and a.id<>(select max(id) from table_test )
lsxaa 2004-10-26
  • 打赏
  • 举报
回复
samuelpan(欧乐) 的正确
samuelpan 2004-10-26
  • 打赏
  • 举报
回复
select *
from table_test a
where not exists(select 1 from table_test where id=a.id+1)
or not exists(select 1 from table_test where id=a.id-1)
and a.id!=(select min(id) from table_test)
and a.id!=(select max(id) from table_test)
samuelpan 2004-10-26
  • 打赏
  • 举报
回复
用游标的话很多事情都可以搞定的。
lsxaa 2004-10-26
  • 打赏
  • 举报
回复
随便可以写成* 和结果没关系的
lsxaa 2004-10-26
  • 打赏
  • 举报
回复
上面的不对 用这个

select *
from table_test a
where (not exists(select 1 from table_test where id=a.id-1 ) and id<>1)
or not exists(select 1 from table_test where id=a.id+1 )
wishton_126 2004-10-26
  • 打赏
  • 举报
回复
lsxaa(小李铅笔刀) 兄


select 1


1 是什么意思啊?
tinghuyang 2004-10-26
  • 打赏
  • 举报
回复
up
lsxaa 2004-10-26
  • 打赏
  • 举报
回复
select *
from t a
where not exists(select 1 from t where id=a.id-1 and id=a.id+1)

wishton_126 2004-10-26
  • 打赏
  • 举报
回复
我的意思 是 输出 ID不连续的记录

zjcxc 2004-10-25
  • 打赏
  • 举报
回复
看不明白,3,11不连续怎么又关11,12连续的事

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧