SQL查询10条数据
查询前10条数据
MySQL、PostgreSQL使用limit
select * from 表名 order by id(主键)limit 10
sql server使用top
select top (10) * from表名
Oracle使用rownum
select * from 表名 where rownum <=10
查询第11条到第15条数据
MySQL使用 limit m,n
select * from 表名 limit 10,5
PostgreSQL使用 limit m offset n
select * from 表名 limit 5 offset 10
SQL Server使用top
select top 5 * from 表名 where 排序字段not in (select top 10 排序字段from 表名)
Oracle使用rownum
select * from (select 字段,rownum rn from 表名where rownum <= 15)where rn >= 10;
最佳选择:利用分析函数
以上四种数据库都支持
select row_number() over(ORDER BY 字段 asc) as rownumber,* from 表名
这样查出来的表中就会多一列“rownumber”,从1开始递增。
这就好办了,在外面嵌套一层就可以“为所欲为”了,这里写一个PostgreSQL的例子。
select * from (select “row_number”() over(ORDER BY 字段 asc) as rownumber,* from “表名”) as temp_row where rownumber>10 limit 5;
你也可以写成“where rownumber>10 and rownumber<=15”
sqlserver就前面加“top (5)”,后面“where rownumber>10”