SQL SERVER查询问题(100)

  • 主题发起人 主题发起人 driayu
  • 开始时间 开始时间
D

driayu

Unregistered / Unconfirmed
GUEST, unregistred user!
在SQL SERVER中,我要查询Autoid值 在以下范围的所有记录200至300,500,600,523,698, 900至1200请各位高手帮我完成下面这个语句Select * from TableName Where Autoid in (????)
 
不明白你所说的范围是怎样划分的,给你一个参考语句:Select * from TableName Where Autoid between 200 and 300UnionSelect * from TableName Where Autoid between 500 and 600UnionSelect * from TableName Where Autoid between 523 and 698UnionSelect * from TableName Where Autoid between 900 and 1200Order By Autoid祝你好运。
 
我的意思是,能不能把这些范围或值,全部放在括号内?
 
你用in语句只能得到等于那些值的记录而不是在范围内的数据,关键还是看你的要求,你也可以试试用下面的写法:Select * from TableName Where (Autoid between 200 and 300) or (Autoid between 500 and 600) or (...)
 
Select * from TableName Where (Autoid between 200 and 300) or (Autoid=500) or (Autoid=523) or (Autoid=600) or (Autoid=698) or (Autoid between 900 and 1200)
 
Select * from TableName Where Autoid in (select autoid from tablename where Autoid between 200 and 300 or Autoid=500 or Autoid=523 or Autoid=600 or Autoid=698 or Autoid between 900 and 1200)
 
我想LZ说的应该是类似这样的语句:Select * from TableName Where Autoid in (200..300,500,600,523,698, 900..1200)就是有些类似DELPHI中的一种语法。但sql server不支持这样的写法。
 
sql server不支持这样的写法,如果你的SQL是动态生成的,可以考虑使用临时表,将你要查询的ID循环到#t1中,然后再关联查询Select * from TableName a,#t1 b Where a.Autoid = b.Autoid
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
491
import
I
后退
顶部