34,838
社区成员




select * from WS_Video where ABS(V_ID-@V_ID) <3 and V_ID <> @V_ID and v_id<>@V_ID and V_type=@type order by V_ID asc
这个写法不行 如果 [V_ID] 不连续就挂了..declare @t table(ID int )
insert @t select 1
insert @t select 2
insert @t select 3
insert @t select 5
insert @t select 6
insert @t select 7
insert @t select 8
insert @t select 9
insert @t select 10
insert @t select 11
insert @t select 12
declare @id int
set @id=10
;
with cte1 as
(
select *,flag=row_number() over (order by id) from @t
)
select id from cte1
where flag between (select flag from cte1 where id=@id)-2 and (select flag from cte1 where id=@id)+2 and id<>@id
/*
id
-----------
8
9
11
12
*/
set nocount on
declare @t table(ID int )
insert @t select 1
insert @t select 2
insert @t select 5
insert @t select 6
insert @t select 7
insert @t select 8
insert @t select 9
insert @t select 10
insert @t select 11
insert @t select 12
declare @id int
set @id=5
select * from (select top 2 * from @t where id<@id order by id desc) t
union all
select * from (select top 2 * from @t where id>@id order by id) t
/*
ID
-----------
2
1
6
7
*/
set @id=10
select * from (select top 2 * from @t where id<@id order by id desc) t
union all
select * from (select top 2 * from @t where id>@id order by id) t
/*
ID
-----------
9
8
11
12
*/
DECLARE @TB TABLE([COL] INT)
INSERT @TB
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 5 UNION ALL
SELECT 6 UNION ALL
SELECT 7 UNION ALL
SELECT 8 UNION ALL
SELECT 9 UNION ALL
SELECT 10 UNION ALL
SELECT 11 UNION ALL
SELECT 12
SELECT *
FROM @TB AS T
WHERE EXISTS(SELECT 1 FROM @TB WHERE COL>=T.COL AND COL<5 HAVING COUNT(*) BETWEEN 1 AND 2)
OR EXISTS(SELECT 1 FROM @TB WHERE COL>5 AND COL<=T.COL HAVING COUNT(*) BETWEEN 1 AND 2)
/*
COL
-----------
2
3
6
7
*/
select identity(int,1,1) as id, A into # from tb
declare @n int
set @n=5
select A
from #
where A<>@n and ID between (select ID from # where A=@n )-2 and (select ID from # where A=@n )+2
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb( a int )
go
insert tb SELECT
1 UNION ALL SELECT
2 UNION ALL SELECT
3 UNION ALL SELECT
5 UNION ALL SELECT
6 UNION ALL SELECT
7 UNION ALL SELECT
8 UNION ALL SELECT
9 UNION ALL SELECT
10 UNION ALL SELECT
11 UNION ALL SELECT
12
go
select identity(int,1,1) as id, A into # from tb
declare @n int
set @n=6
select A
from #
where ID between (select ID from # where A=@n )-2 and (select ID from # where A=@n )+2
and A<>@n
go
/*------------
A
-----------
3
5
7
8
-------*/
set nocount on
declare @t table(ID int )
insert @t select 1
insert @t select 2
insert @t select 3
insert @t select 5
insert @t select 6
insert @t select 7
insert @t select 8
insert @t select 9
insert @t select 10
insert @t select 11
insert @t select 12
declare @id int
set @id=5
select top 4 * from @t where id!=@id order by abs(@id-id)
set @id=10
select top 4 * from @t where id!=@id order by abs(@id-id)
/*ID
-----------
6
7
3
2
ID
-----------
9
11
8
12*/