34,591
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int, content varchar(20), users varchar(20))
insert tb select 1 ,'XXXXX' ,'user1'
union all select 5 ,'XXXXX' ,'user2'
union all select 10 ,'XXXXX' ,'user3'
union all select 13 ,'XXXXX' ,'user2'
union all select 15 ,'XXXXX', 'user2'
union all select 20 ,'XXXXX' ,'user3'
select * from(
select * from
(
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
union all
select top 1 * from tb where users = 'user2' and id<13 order by id desc) t1
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id<13))
union all
select * from
(
select * from
(select top 1 * from tb where users = 'user2' and id>13 order by id) f
union all
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
)t2
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id>13))
) t
/*id content users
----------- -------------------- --------------------
5 XXXXX user2
15 XXXXX user2
(所影响的行数为 2 行)
*/
select * from(
select * from
(
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
union all
select top 1 * from tb where users = 'user2' and id<5 order by id desc) t1
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id<5))
union all
select * from
(
select * from
(select top 1 * from tb where users = 'user2' and id>5 order by id) f
union all
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
)t2
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id>5))
) t
/*id content users
id content users
----------- -------------------- --------------------
0 0 0
13 XXXXX user2
(所影响的行数为 2 行)
*/
select * from(
select * from
(
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
union all
select top 1 * from tb where users = 'user2' and id<15 order by id desc) t1
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id<15))
union all
select * from
(
select * from
(select top 1 * from tb where users = 'user2' and id>15 order by id) f
union all
select convert(varchar(10),0) id,convert(varchar(10),0) content,convert(varchar(10),0) users
)t2
where id<>0 or (id=0 and not exists(select 1 from tb where users = 'user2' and id>15))
) t
/*
id content users
----------- -------------------- --------------------
13 XXXXX user2
0 0 0
(所影响的行数为 2 行)
*/
select * from(
select 0,0,0
union all
select top 1 * from tb where users = user2 and id<13 order by id desc
union all
select top 1 * from tb where users = user2 and id>13 order by id
union all
select 0,0,0
)t
create table b_(id int, content varchar(20), users varchar(20))
insert b_ select 1 ,'XXXXX' ,'user1'
union all select 5 ,'XXXXX' ,'user2'
union all select 10 ,'XXXXX' ,'user3'
union all select 13 ,'XXXXX' ,'user2'
union all select 15 ,'XXXXX', 'user2'
union all select 20 ,'XXXXX' ,'user3'
select * from (
select top 1 * from (
select * from (select top 1 * from b_ where users='user2' and id<15 order by id desc) aa
union all
select null,null,null
)aaa order by id desc
)aaaa
union all
select * from (
select top 1 * from (
select * from (select top 1 * from b_ where users='user2' and id>15 order by id) aa
union all
select null,null,null
)aaa order by id desc
) bbbb
--result
/*id content users
----------- -------------------- --------------------
13 XXXXX user2
NULL NULL NULL
(所影响的行数为 2 行)
*/
select top 1 * from tb where users = user2 ,id<13 desc
union all
select top 1 * from tb where users = user2 ,id>13 desc