假设我有一个表格,其列表如下:
SELECT Col1
FROM table;
Col1
A
A
B
B
C
C
D
B
E
B
F
我想输出介于" B"之间的元素,即C,D,E 我怎么能用查询呢?
答案 0 :(得分:1)
declare @t table (ID INT IDENTITY(1,1),col1 VARCHAR(10))
insert into @t (col1) values ('A')
insert into @t (col1) values ('A')
insert into @t (col1) values ('B')
insert into @t (col1) values ('B')
insert into @t (col1) values ('C')
insert into @t (col1) values ('C')
insert into @t (col1) values ('B')
insert into @t (col1) values ('E')
insert into @t (col1) values ('B')
insert into @t (col1) values ('F')
select ID,col1 from @t
where ID between (select MIN(id) from @t WHERE col1 = 'B') and
(select MAX(id) from @t WHERE col1 = 'B')
and col1<>'B'