50分求一简单SQL语句

523shan 2005-11-17 11:03:30
一个表有10条记录,现在想取‘去掉前三个记录后‘的前四个记录,sql语句改怎么写?谢谢
...全文
83 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jertstre 2005-11-17
  • 打赏
  • 举报
回复
select top 4 * from(select top 4 * from(select top 7 * from AA)as b order by testid desc) as a order by testid asc
zoubsky 2005-11-17
  • 打赏
  • 举报
回复
--测试代码
declare @tb1 table(id int identity(1,1),score int)
insert @tb1
select 11 union all
select 22 union all
select 33 union all
select 44 union all
select 55 union all
select 66 union all
select 77 union all
select 88 union all
select 99 union all
select 100

select * from @tb1 where id not in(select top 3 id from @tb1 order by id asc) and id not in (select top 4 id from @tb1 order by id desc)

/*
测试结果
id score
----------- -----------
4 44
5 55
6 66
*/
amtyuranus 2005-11-17
  • 打赏
  • 举报
回复
晕!!

select top 4 from (select * from table where id not in (1,2,3))
523shan 2005-11-17
  • 打赏
  • 举报
回复
谢谢,结了
jxdjxd1111 2005-11-17
  • 打赏
  • 举报
回复
select id,cc from AA where id between 4 and 7

jxdjxd1111 2005-11-17
  • 打赏
  • 举报
回复
create table tb(number varchar(20),dt datetime)
insert into tb
select '191', '2005-11-17 10:39:50.217' union
select '192', '2005-11-18 10:40:10.077' union
select '193', '2005-11-19 10:40:11.327' union
select '194', '2005-11-20 10:40:12.327' union
select '105', '2005-11-21 10:40:21.590' union
select '106', '2005-11-22 10:40:28.250' union
select '107', '2005-11-23 10:40:29.483' union
select '108', '2005-11-24 10:40:21.590' union
select '109', '2005-11-25 10:40:28.250' union
select '110', '2005-11-26 10:40:29.483'




select IDENTITY(int, 1,1) as id,number,dt into #b from tb

select id,number,dt from #b where id between 4 and 7

drop table #b
drop table tb

结果
id number dt
4 108 2005-11-24 10:40:21.590
5 109 2005-11-25 10:40:28.250
6 110 2005-11-26 10:40:29.483
7 191 2005-11-17 10:39:50.217
wangdehao 2005-11-17
  • 打赏
  • 举报
回复
select top 4 * from tablename where id not in(select top 3 id from tablename order by id )order by id asc
523shan 2005-11-17
  • 打赏
  • 举报
回复
谢谢 yinxiuwen,刚才说的不清楚,我是举例说表里有10条记录,不是一定有10条
jertstre 2005-11-17
  • 打赏
  • 举报
回复
select top 4 * from(select top 7 * from tablename order by id desc) as b order by id asc
523shan 2005-11-17
  • 打赏
  • 举报
回复
例如表AA如下

ID CC
1 11
2 22
3 33
4 44
5 55
6 66
7 77
8 88
9 99
10 00

现在想取‘去掉前三个记录后‘的前四个记录,就是
ID CC
4 44
5 55
6 66
7 77

SQL怎么写?各位大大帮帮忙

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧