0
点赞
收藏
分享

微信扫一扫

[数据库汇总]-- 数据库分页查询技术以及实现查询随机数


一、oracle 分页支持:rownum关键字

 select s.* from ( select p.*,rownum rm  from ( select  t.* from t_user t) p ) s where rm between 2 and 8 ;

注意查询时,用问号替代

select s.* from ( select p.*,rownum rm  from ( select  t.* from t_user t) p ) s where rm between ? and ? ;

pstm.setInt(1, (nowPage-1)*sizePage+1);

pstm.setInt(2, nowPage*sizePage);

备注:随机查询的产生

select t.* from (select s.* ,rownum rm from (select * from t_category where c_level=2  order by dbms_random.value

二、mysql 分页支持:limit关键字

select * from t_user limit 2,8;

注意查询时,2和8用占位符号替代

select * from t_user limit ?,?;

 pstm.setInt(1, (nowPage-1)*sizePage);  //代表nowPage下的第一条开始数据
         pstm.setInt(2, nowPage*sizePage);       //代表sizePage下的最后一条数据

备注:mysql随机查询

select * from course35 where cname='数学'order by rand()

三、sql server分页支持 :top关键字

select top 10 *  from 表名                                                                              --查询显示0-10条记录(10条)
select top 10 * from 表名 where 主键 not in(select top 10 表名 from 主键); --查询显示11-20条记录(10条)
select top 10 * from 表名 where 主键 not in(select top 20 表名 from 主键); --查询显示21-30条记录(10条)
select top 10 * from 表名 where 主键 not in(select top 30 表名 from 主键); --查询显示31-40条记录 (10条)

备注:sql server 随机查询

select top 10 * from t_user order by newid();

 

举报

相关推荐

0 条评论