sql语句找出缺少的数

来源:互联网 发布:淘宝宝贝拍照 编辑:程序博客网 时间:2024/04/30 04:56

数据表记录,一列的数形如:2,3,4,6,7,8,10,11
1>请用一句sql把中间第一个缺少的数(5)找出来
2>请用一句sql把中间所有缺少的数找出来(5,9)

 

 

1.
select (min(num_column) - 1) as num_column from num_table where not num_column in(select (num_column + 1) from num_table) and num_column <> 2

2.
select (num_column - 1) as num_column from num_table where not num_column in(select (num_column + 1) from num_table) and num_column <> 2

 

 

 

如果不考虑到性能的话:
1>请用一句sql把第一个缺少的数(5)找出来(考虑上面的数据列是按顺序的,如果是非顺序的话,需要另外考虑)
select min(num)+1 as result from table where num+1 not in (select num from table )

2>请用一句sql把中间所有缺少的数找出来(5,9)
select num+1 as result from table where num+1 not in (select num from table )

原创粉丝点击