您可以在子查询中使用analytic function 之类的dense_rank() 对ID 进行排名,然后根据您想要的排名进行过滤,例如:
select id, col1, col2
from (
select id, col1, col2, dense_rank() over (order by id) as rnk
from list_items
)
where rnk <= 10
或用于以后的页面
select id, col1, col2
from (
select id, col1, col2, dense_rank() over (order by id) as rnk
from list_items
)
where rnk > 10 and <= 20
如果列表中的 ID 没有 ID,并且您希望将其考虑在内,则可以对该表使用子查询并连接(这也允许您包含其他列表列):
select l.id, li.col1, li.col2
from (
select id, dense_rank() over (order by id) as rnk
from list
) l
left join list_items li on li.id = l.id
where l.rnk <= 10;
如果您使用的是 Oracle 12c 或更高版本,则可以使用 [行限制子句] 增强功能来简化:
select l.id, li.col1, li.col2
from (
select id
from list
order by id
fetch next 10 rows only
) l
left join list_items li on li.id = l.id;
或第二页:
offset 10 rows fetch next 10 rows only