sql语句查询某条记录

来源:百度知道 编辑:UC知道 时间:2024/09/22 09:55:08
我要取第N条记录 请问应该怎么做

select * top 1 from table where id not in (select id top n from table order by id) order by id

除非记录中有个索引字段,要不直接用sql语句是不能实现的。

select [列名] from <表名> 后面可以加 where 条件

首先。我们知道。
如果要查询 前 2行的话。只需select top 2 字段名1 from tablename
如果要查询 前 3行的话。只需select top 3 字段名1 from tablename
那么要查询 第 3行的话。就可以这样做:
select 字段名1 from tablename
where (select top 3 字段名1 from tablename ) not in (select top 2 字段名1 from tablename)
----
自然的,当我们想查询第n行的时候,是否只需要把对应的3改为n,把2改为n-1呢?
但是select top n from tablename 语句对n的要求是n必须是确定的数值才可以!
所以如果这样写的话那肯定就会报错:
declare @i int,@j int
set @i=1
set @j=@i-1
select 字段名1 from tablename
where (select top @i 字段名1 from tablename ) not in (select top @j 字段名1 from tablename)
------------------
那么应该怎样才不会报错呢?
这里要用到exec()。。。

declare @i int
declare @j int
set @i=13
set @j=@i-1
exec(' declare @c char(20) '+' select @c=字段名1 from jw_xsb where 字段名1 i