当前位置:首页 » 编程语言 » sql怎么查看第m条
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql怎么查看第m条

发布时间: 2022-07-20 17:09:21

Ⅰ 如何在sql中查询第N条到第M条数据

with cte as ( select RowNumber=row_number() over(order by id),* from 表名 ) select * from cte where RowNumber between 10 and 20 这是查10到20行的

Ⅱ sql语言中查询第n行第m列怎么表达

sql="select * from table m='"&m&"'and n="&n
n表示你要查询的行数
m表示你要查询的列名

Ⅲ 如何查询得到SQL数据库表中第N行数据,N是一个参数,不确定的int数

sql server里好像没有查询特定行的关键字,可以试试:select top 1 * from 表名 where 表的主键字段 not in (select top 参数-1 表的主键字段 from 表名)

例如:
表名:test
id int primary key,
name varchar 50.

想要查询第50行的记录:
select top 1 * from test where id not in (select top 49 id from test)

还可以根据需要进行排序。

Ⅳ 如何在SQL Server中查找从N条记录开始的M条记录查询语句该怎么写

首先在ID上创建聚集索引是必须的。
如果ID是连续的,楼上的的用id来限定语句范围是很快的。
如果id不是连续的,楼主的语句使用了not
in,一般这样效率是很低的。
可以把语句改成
select
top
M
*
from
table
where
id>
(select
max(id)
from
(select
top
N
id
from
table
order
by
id)
a)
order
by
id
这样可以一定程度上提高一些性能。
比如我的表中有300多万条记录,使用楼主的语句执行时间需要9秒多,而用我的语句3秒多。

Ⅳ sql如何使用游标查询指定行记录

可以
不过不用游标也可以

select * from (select *,row_number() over(order by 排序列) as rownum from table) a
where rownum>=n and rownum<=m

Ⅵ sql获取第n条记录中的第m个数据

你是已经查询出来了很多行。
但是只要 第一行数据?

Oracle 的话:

SELECT
*
FROM
(
你的那个查询
)
WHERE
ROWNUM = 1;

DB2 的话:
你的那个查询

fetch first 1 rows only;

SQL Server 的话:
SELECT TOP 1 你的查询的其他列 FROM 你的表 WHERE 你的条件

MySQL 的话:

你的那个查询
LIMIT 1;

Ⅶ sql 取中间几条记录

--从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
SELECT TOP n-m+1 * FROM Table WHERE (id NOT IN (SELECT TOP m-1 id FROM Table ))
--从TABLE表中取出第m到n条记录 (Exists版本)
SELECT TOP n-m+1 * FROM TABLE AS a WHERE Not Exists
(Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id )
Order by id
--m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名
Select Top n-m+1 * From Table
Where Id(Select Max(Id) From
(Select Top m-1 Id From Table Order By Id Asc) Temp)
-----------------------------------------------------------------------------------------------------------------------------------------
表pictures中有两个字段:id与title。id是自动编号的­
表中有5条记录:1--p1,2--p2,3--p3,4--p4,5--p5­
一、找到了一个小规律
string sqlstr = "select top 4 * from pictures order by id desc "; //查询结果p5,p4,p3,p2---说明是整个表先进行排序,再进行查询的­
string sqlstr = "select top 3 * from (select top 4 * from pictures order by id desc) "; //-------p5,p4,p3­
string sqlstr = "select top 3 * from (select top 4 * from pictures order by id desc) order by id desc";//-------p5,p4,p3
string sqlstr = "select top 3 * from (select top 4 * from pictures order by id desc) order by id asc"; //-------p2,p3,p4
二、获取单条记录:­
假设表中一共有counts条记录,现在想要查询第n条记录,则sql语句应是:­
select top 1 * from (select top (counts-n+1) * from pictures order by id desc) order by id asc­
第三条记录:­
string sqlstr = "select top 1 * from (select top 3 * from pictures order by id desc) order by id asc";//-------p3­
三、获取表中多条连续的记录­
假设表中一共有counts条记录,现在想要查询第n到第m条的记录,则sql语句应是:­
select top (m-n+1) * from (select top (counts-n+1) * from pictures order by id desc) order by id asc­
获取第二到第四条记录:­

Ⅷ sql语句查询结果只取从第m条开始到第n条结束请问该怎么做

取n到m行

1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)

2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc

3.
select top n * from
(select top m * from tablename order by columnname) a
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

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m

Ⅸ 查询第m到n行的sql语句 数据库是ACCESS m,n是变量。

如果有一张表userinfo(user_id,user_name,desc),则该表第m到n行的sql语句为:
select user_id,user_name,desc from (select top n user_id,user_name,desc,row_number() over(order by user_id) as rowindex from userinfo) t where t.rowindex>=m;

以上若还有疑问,可以Hi我。