34,588
社区成员
发帖
与我相关
我的任务
分享
--原始数据:@4
declare @4 table(A int,B int,C int,D int)
insert @4
select 1,3,5,8 union all
select 5,6,7,8 union all
select 1,2,3,4 union all
select 2,9,8,7 union all
select 3,7,6,5 union all
select 4,8,7,6 union all
select 8,9,0,4 union all
select 5,4,3,2
--必须有唯一标识数据的字段如标识列
declare @T table(id int identity,A int,B int,C int,D int)
insert @T select * from @4
--正序:5,1,2,3
select A,B,C,D from @T a
where exists (select 1 from @T where id=a.id-4 and A=5)
and exists (select 1 from @T where id=a.id-3 and A=1)
and exists (select 1 from @T where id=a.id-2 and A=2)
and exists (select 1 from @T where id=a.id-1 and A=3)
/*
A B C D
----------- ----------- ----------- -----------
4 8 7 6
*/
--逆序:3,4,8,5
select A,B,C,D from @T a
where exists (select 1 from @T where id=a.id+1 and A=3)
and exists (select 1 from @T where id=a.id+2 and A=4)
and exists (select 1 from @T where id=a.id+3 and A=8)
and exists (select 1 from @T where id=a.id+4 and A=5)
/*
A B C D
----------- ----------- ----------- -----------
2 9 8 7
*/
create table tb(A int,B int,C int,D int)
insert into tb values(1, 3, 5, 8 )
insert into tb values(5, 6, 7, 8 )
insert into tb values(1, 2, 3, 4 )
insert into tb values(2, 9, 8, 7 )
insert into tb values(3, 7, 6, 5 )
insert into tb values(4, 8, 7, 6 )
insert into tb values(8, 9, 0, 4 )
insert into tb values(5, 4, 3, 2 )
go
select id=identity(int,1,1), * into tmp from tb
--顺序查
declare @n1 as int
declare @n2 as int
declare @n3 as int
declare @n4 as int
set @n1 = 5
set @n2 = 1
set @n3 = 2
set @n4 = 3
select a,b,c,d from tmp where id in
(
select d.id + 1 from tmp a,tmp b,tmp c ,tmp d
where a.a = @n1 and
a.id = b.id - 1 and b.a = @n2 and
a.id = c.id - 2 and c.a = @n3 and
a.id = d.id - 3 and d.a = @n4
)
/*
a b c d
----------- ----------- ----------- -----------
4 8 7 6
(所影响的行数为 1 行)
*/
--反序查
declare @t1 as int
declare @t2 as int
declare @t3 as int
declare @t4 as int
set @t1 = 3
set @t2 = 4
set @t3 = 8
set @t4 = 5
select a,b,c,d from tmp where id in
(
select a.id - 1 from tmp a,tmp b,tmp c ,tmp d
where a.a = @t1 and
a.id = b.id - 1 and b.a = @t2 and
a.id = c.id - 2 and c.a = @t3 and
a.id = d.id - 3 and d.a = @t4
)
/*
a b c d
----------- ----------- ----------- -----------
2 9 8 7
(所影响的行数为 1 行)
*/
drop table tb,tmp
create table tb(A int,B int,C int,D int)
insert into tb values(1, 3, 5, 8 )
insert into tb values(5, 6, 7, 8 )
insert into tb values(1, 2, 3, 4 )
insert into tb values(2, 9, 8, 7 )
insert into tb values(3, 7, 6, 5 )
insert into tb values(4, 8, 7, 6 )
insert into tb values(8, 9, 0, 4 )
insert into tb values(5, 4, 3, 2 )
go
declare @n1 as int
declare @n2 as int
declare @n3 as int
declare @n4 as int
set @n1 = 5
set @n2 = 1
set @n3 = 2
set @n4 = 3
select id=identity(int,1,1), * into tmp from tb
select a,b,c,d from tmp where id in
(
select d.id + 1 from tmp a,tmp b,tmp c ,tmp d
where a.a = @n1 and
a.id = b.id - 1 and b.a = @n2 and
a.id = c.id - 2 and c.a = @n3 and
a.id = d.id - 3 and d.a = @n4
)
drop table tb,tmp
/*
a b c d
----------- ----------- ----------- -----------
4 8 7 6
(所影响的行数为 1 行)
*/