sql第到第
① 在sql SERVER中查询数据库中第几条至第几条之间的数据SQL语句怎么写
在sql
server中查询数据库中第几条至第几条之间的数据sql语句如何写?
如:在sql
server中查询数据库中第10条至30条之间的数据sql语句如何写?
------解决方案--------------------
select
top
20
*
from
表
where
id
in
(select
top
30
id
from
表
order
by
id)order
by
id
desc
------解决方案--------------------
如果有唯一列可以用ls的
select
identity(int,1,1)
id,*
into
temp
from
表
select
*
from
temp
where
id
between
10
and
30
------解决方案--------------------
select
top
20
*
from
表
where
标识字段
not
in
(select
top
9
标识字段
from
表
)
------解决方案--------------------
1
select
top
20
*
from
表
where
id
not
in
(select
top
10
id
from
表
order
by
id)
order
by
id
2--应该从11开始
select
*
from
表
where
id
between
11
and
30
② SQL返回第M条至第N条记录
总结的查找从第N条到第M条记录,希望对你有帮助:
查找第n到m条记录:
(1)select top m * from tablename where id not in (select top n id from tablename)
此语句需要在表有主键类字段,此句里的为id
(2) select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量 order by columnname desc
(3)select top n * from
(select top m * from tablename order by columnname) order by columnname desc
(4)如果tablename里没有其他identity列,那么:
select identity(int) id0,* into #temp from tablename
取n到m条的语句为:
select * from #temp where id0 >=n and id0 <= m
如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你 的 DB中间的select into/bulk属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulk',true
(5).如果表里有identity属性,那么简单:
select * from tablename where identity col between n and m
③ 用SQL如何查询第几条到第几条之间的数据想不出来呢,网上的答案都不行的
这好办,比如查询第10条到第20条数据,表是table1,列是lie1,lie2,那就是
select top 20 from table1 where lie1 not in(select top 10 lie1 from table1)
括号里是查出前10条数据,然后查出所以的前20条数据,排除前10条,就是第10条到第20条了啊!这是我们一直在用的方法,思路很清晰、、、
④ 在SQL SERVER中查询数据库中第几条至第几条之间的数据SQL语句怎么写
1、首先我们先来看一下查询语句的like优化,如下图所示,分别将百分号放在前面和后面。