sql 查询第30条数据到40条数据怎么查啊,id有可能不是连续的

2024-12-01 03:25:08
推荐回答(5个)
回答1:

假设数据所在的表为A,查询语句辅助的表T,表W。
SQL语句查询逻辑过程为首先选出前40条记录,然后按照递减顺序查询后11条记录,也就是从第40条,第39条,...,第30条记录。为了还原为原来数据的递增顺序,可以再嵌套个SELECT语句。
SELECT * FROM (SELECT TOP 11 * FROM (SELECT TOP 40 * FROM A ORDER BY id ASC) T ORDER BY T.id DESC) W ORDER BY W.id ASC

回答2:

mysql 可以用limit 30, 10
mssql 没法直接取
可以先 select max(id) as id from table where id in (select top 40 id from table)
然后在 select top 10 * from table where id < (上面的max) order by id desc

回答3:

修改
select top 40* from table where 字段名not in (select top 30 字段名 from table)

回答4:

oracle 可以用rownum
select *
from (select rownum r,*
from table)
where r between 30 and 40

回答5:

select top 10 * table
where id in ( select top 40 id from table order by id )
and id not in ( select top 30 id from table order by id )