无双森兰丸是男的:SQL数据库提取问题

来源:百度文库 编辑:高考问答 时间:2024/04/30 02:23:12
我如何实现仅提取数据库的某几条记录?
比如
select top 100 * from table
这是提取前100条
我现在要提取第100条到第200条呢?

麻烦帮助解答~谢谢
比如说我现在做了一个分页程序
他每次都提取所有数据记录然后按分页要求显示
假设每页显示20条记录
那我要每次仅提取当前页的20条记录怎么实现。而不是全部打开~

首先你的数据库要有可以唯一排序的字段,比如ID或者编号之类。

假定你用id进行唯一排序,那么提取第100到第200条记录的语句就是:

select top 100 * from table where id>(select max(id) from (select top 100 id from table order by id asc) as tblTmp) order by id asc

假定你每页显示数量为Point,当前页码为Page,那么显示该页所有记录的SQL语句应该是:

sql = "select top " & Point & " * from table where id>(select max(id) from (select top " & (Page - 1) * Point & " id from table order by id asc) as tblTmp) order by id asc

假设id是主键:
select *
from (select top 200 * from yourtable) aa
where not exists(select 1 from (select top 100 * from yourtable) bb where aa.id=bb.id)