日期:2014-05-18  浏览次数:20637 次

提取数据段问题(急啊)
表A为:
Message(字段名)
dj
OK
djflk
ieo
po
ew
ww
x
YES
m
3
t
OK
we
sff
jj
kk
YES
jfkdj
要查询出从OK到YES的所有行(注:OK到YES的数据段有多段)
请问SQL语句怎么写????

------解决方案--------------------
--测试数据
create table a(Message varchar(20))
insert into a select 'dj ' union all
select 'OK ' union all
select 'djflk ' union all
select 'ieo ' union all
select 'po ' union all
select 'ew ' union all
select 'ww ' union all
select 'x ' union all
select 'YES ' union all
select 'm ' union all
select '3 ' union all
select 't ' union all
select 'OK ' union all
select 'we ' union all
select 'sff ' union all
select 'jj ' union all
select 'kk ' union all
select 'YES ' union all
select 'jfkdj '

--查询
select id=identity(int,1,1),Message into # from a

select message from (select Message,(select count(1) from # where message= 'OK ' and id <=t.id)-(select count(1) from # where message= 'YES ' and id <t.id) as tp from # t) tt where tp=1

--删除临时表
drop table #


--结果
OK
djflk
ieo
po
ew
ww
x
YES
OK
we
sff
jj
kk
YES