当前位置:首页 » 编程语言 » sql第到第

sql第到第

发布时间: 2023-07-17 23:10:50

① 在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优化,如下图所示,分别将百分号放在前面和后面。

热点内容
cod17编译着色器55 发布:2025-07-08 15:43:53 浏览:558
Shell脚本的posix模式 发布:2025-07-08 15:41:32 浏览:352
压缩奶油消泡 发布:2025-07-08 15:40:11 浏览:425
服务器一定要有公网ip吗 发布:2025-07-08 15:35:12 浏览:797
appendpython 发布:2025-07-08 15:22:54 浏览:656
安卓虚拟号码怎么设置 发布:2025-07-08 15:22:04 浏览:663
c语言爱心代码编译不出来 发布:2025-07-08 15:11:07 浏览:540
qq密码的数据库 发布:2025-07-08 14:54:50 浏览:6
多图床源码 发布:2025-07-08 14:46:36 浏览:573
sqldblinkoracle 发布:2025-07-08 14:44:50 浏览:608