`
binyan17
  • 浏览: 199898 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

【转】玩转三种主流数据库(oracle,sql server,mysql)web翻页

 
阅读更多

 

原文:http://blog.sina.com.cn/s/blog_4c925dca0100jvpd.html

于web上实现上翻页功能,阿堂前面在新浪博客(http://blog.sina.com.cn/heyitang)和阿堂的qq空间上都有详细介绍(我已经写过三遍關于各种情况下的博文),我这里就不再重复介绍了,有兴趣的朋友可以去我的博客上参考相关文章!
  今天,阿堂所要说的,对于目前web上使用的三大主流数据库,oracle,sql server,mysql中通过sql语句来达到定制显示我们需要的记录数,如我要显示业务sql中的,第11行到20行的记录,如果我们能在sql语句中,做到了这一点,在web上实现分页,就是一件很轻松的事情了,所以,阿堂下面主要就是对这三种主流数据库的对应的sql的写法进行总结了!如果,朋友们认真体会,阿堂写的这遍文章的话,完全就可以自己来写个通用的翻页封装类了!(朋友们如果是想更详细的了解sql语句,如何在web上实现分页更详细的过程,请参考阿堂上面写的几遍文章即可,如有任何问题,欢迎给阿堂留言,我会及时进行解答的!)

第一种情况 Oralce
 如
select * from(
    select my_table.*, rownum as my_rownum from
    (
        (业务sql语句)  my_table
            where rownum<=20
    ) where my_rownum>=11
)

 

说明:这里主要利用了oracle的rownum的功能

测试代码
select * from
(
    select my_table.*, rownum as my_rownum from
    (
                    select * from (
               select lot.sku,lot.storerkey storer,to_char(lotattr.lottable05,'YYYY-MM-DD hh24:mi:ss') receipt_date, 
                      sku.descr,trunc(sysdate-lotattr.lottable05) as again, 
                      sum(lot.qty-lot.qtyallocated) as qty, 
                      case lotattr.lottable09 when 'O' then '合格' 
                                                                  when 'I' then '待检' 
                                                                  when 'H' then '不合格' 
                      end as status, 
                      getunitofpackwh9(pack.packdescr) as Unit 
               from wh9.lot lot, 
                    wh9.Lotattribute lotattr, 
                    wh9.sku sku, 
                    wh9.pack pack 
               where lot.lot = lotattr.lot 
                 AND lot.sku = sku.sku 
                 AND lot.storerkey = sku.storerkey 
                 AND lotattr.lottable01 = pack.packkey 
                 AND lot.qty-lot.qtyallocated>0                
               group by lot.sku,lot.storerkey,to_char(lotattr.lottable05,'YYYY-MM-DD hh24:mi:ss'),sku.descr, 
                        trunc(sysdate-lotattr.lottable05), 
                        case lotattr.lottable09 when 'O' then '合格' 
                                                                    when 'I' then '待检' 
                                                                    when 'H' then '不合格' 
                        end,getunitofpackwh9(pack.packdescr) 
            )  where again>1 order by receipt_date desc,status desc,again desc
    ) my_table
    where rownum<=20

 where my_rownum >=11
测试效果图

玩转三种主流数据库(oracle,sql <wbr>server,mysql)web翻页

第二种情况 sql server

//查询第11行到第20行记录
select top 10 * from [表名] where [主键] not in (select top 10 [主键] from [表名] order by
[排序字段及排序方法]) order by [排序字段及排序方法]
说明:这里是利用了主键,如果是多表关联时,上面的 主键选能唯一标识该记录行的字段即可

测试代码
select top 10 * from web_t_booking
 where order_no not in (select top 10 order_no from web_t_booking order by order_no desc)
 order by order_no desc
测试效果图

玩转三种主流数据库(oracle,sql <wbr>server,mysql)web翻页

第三种情况 mysql
我们可以利用MySQL中Select支持的一个子句——LIMIT——来完成这项功能。
LIMIT可以实现top N查询,也可以实现M至N(某一段)的记录查询,具体语法如下:
Select * FROM MYTABLE
order BY AFIELD
LIMIT offset, recnum
其中offset为从第几条(M+1)记录开始,recnum为返回的记录条数。
例:
select * from mytable
order by afield
limit 10, 10
即意为从第11条记录开始的10条记录。

测试代码
select * from address
order by id
limit 10, 10
测试效果图

玩转三种主流数据库(oracle,sql <wbr>server,mysql)web翻页
玩转三种主流数据库(oracle,sql <wbr>server,mysql)web翻页

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics