一个趣味sql 选择 问题(100分)

  • 主题发起人 主题发起人 sunnyboy_delphi
  • 开始时间 开始时间
S

sunnyboy_delphi

Unregistered / Unconfirmed
GUEST, unregistred user!
用一个SQL语句找出从1-100里缺少的最小的数<br>1,2,3,4,9,11,13,16,19,20,100
 
建一个表 A 一个字段,num 把1~20输进去。假设上面的表是B,数字的字段是numb<br>select Min(num) from A where num not in (select numb from B)
 
不用数据库 只用sql 语句
 
1,2,3,4,9,11,13,16,19,20<br>存在哪?[:)]
 
select min(Number) from (<br>select 1 as number where not (1 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 2 as number where not (2 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 3 as number where not (3 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 4 as number where not (4 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 5 as number where not (5 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 6 as number where not (6 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 7 as number where not (7 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 8 as number where not (8 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 9 as number where not (9 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 10 as number where not (10 in (1,2,3,4,9,11,13,16,19,20))<br>union <br>select 11 as number where not (11 in (1,2,3,4,9,11,13,16,19,20))) a
 
libin202<br>&nbsp; &nbsp;如果是改为100 上边的语句是不是太繁琐了一点
 
楼主是不是需要这个sql 语句<br><br>declare @i int <br>set @i=1<br>while (@i&lt;=100)<br>begin<br>&nbsp; if(@i in (1,2,3,4,9,11,13,16,19,20,100))<br>--(1,2,3,4,9,11,13,16,19,20,100) 可以自己添加 但是必须是从小到大排列<br>&nbsp; &nbsp; &nbsp;set @i=@i+1<br>&nbsp; else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; print(cast(@i as char(2))+'这个就是最小的')<br>&nbsp; &nbsp; set @i=101<br>&nbsp; &nbsp;end<br>end
 
select 5<br>最好的计算就是让数据库不用计算
 
多人接受答案了。
 
后退
顶部