Mysql分页查询有limit关键词,分页查询十分方便
例如:--查询领用数量排倒数第5的领用信息
SELECT * from APPLY a order by a.QUANTITY LIMIT 4,1;
--limit两个参数
从第几个开始查,查几个(注意下标从0开始)
查询前3行数据:
select * from emp limit 3;
查询第3到第5行的数据:
select * from emp limit 2,3
查询第5行之后的所有数据(包括第5行):
select * from emp LIMIT 4,14
Oracle分页查询因为没有limit关键字,但是有rownum,rownum其实就是一个隐藏列,记录行号
SELECT * from(
select * from APPLY a order by a.QUANTITY )
where rownum<=5 MINUS (SELECT * from(
select * from APPLY a order by a.QUANTITY )
where rownum<=4)
因为rownum只是一个隐藏列,它不会跟order by排序之后再去自增长,只能用minus去减去
看一个不用order by排序的例子
--查询产品信息表中的第5到第9行信息。
写法1:
SELECT * FROM PRODUCT where ROWNUM <=9 MINUS SELECT * FROM PRODUCT where ROWNUM <5;写法2:
SELECT * from
(
SELECT p.*,rownum rn FROM PRODUCT p
)where rn BETWEEN 5 and 9;
正常表数据顺序
按价格排序后:SELECT p.*,rownum rn FROM PRODUCT p order by p.price
想要去查询价格最低的5-9条数据
SELECT * from
(
SELECT p.*,rownum rn FROM PRODUCT p order by p.price
)where rn BETWEEN 5 and 9; --错误!!
正确写法:
SELECT * from(
SELECT * FROM PRODUCT p order by PRICE) where rownum<=9 MINUS SELECT * from(
SELECT * FROM PRODUCT p order by PRICE) where rownum<=4;
思考:select * from apply where rownum > 5 正确吗?
SELECT * from(
select a.*,rownum as rn from APPLY a )
where rn>5 ;