zoukankan      html  css  js  c++  java
  • oracle中根据当前记录查询前一条和后一条记录

    select * from aa01_2014 where aaa001=(select c.p from (select aaa001,lag(aaa001,1,0)  over (order by aaa001) as p from aa01_2014) c where c.aaa001='8a9299ec522f54f401522f81eedc0007')  ;


    select * from aa01_2014 where aaa001=(select c.n from (select aaa001,lead(aaa001,1,0)  over (order by aaa001) as n from aa01_2014) c where c.aaa001='8a9299ec522f54f401522f81eedc0007') ;
     
     
     

    oracle可以使用 lead、lag  函数来查询已有记录的下一条、上一条记录。

    表结构如下:

    如要查询Staffno是6-1102的前一条记录

    select * from staff where staff_no=(select c.p from (select staff_no,lag(staff_no,1,0)  over (order by staff_no) as p from staff) c where c.staff_no='6-1102')

    结果:

    STAFF_NO   STAFF_NAME           SEX 

    ---------- -------------------- --- -

    6-1076     梁柄聪               男                                                                                                                                                                                                                                                                           

    1 rows selected

    如要查询其后一条记录

    select * from staff where staff_no=(select c.n from (select staff_no,lead(staff_no,1,0)  over (order by staff_no) as n from staff) c where c.staff_no='6-1102')

    结果:

    STAFF_NO   STAFF_NAME           SEX 

    ---------- -------------------- --- -

    6-1103     余志伟               男                                                                                                                    

    1 rows selected

  • 相关阅读:
    C# WPF之Material Design自定义颜色
    C# WPF从RIOT API获取数据(RIOT代表作品《英雄联盟》)
    C# WPF聊天界面(3/3)
    C# WPF简况(2/3)
    C# WPF联系人列表(1/3)
    使用SignalR从服务端主动推送警报日志到各种终端(桌面、移动、网页)
    为什么Node.JS会受到青睐?
    2017级面向对象程序设计——团队作业1
    2017级面向对象程序设计 作业三
    如果抽不出时间写博客怎么办
  • 原文地址:https://www.cnblogs.com/wjwen/p/5135187.html
Copyright © 2011-2022 走看看