oracle的笔记,凑合着看吧! 2 简单的运用技巧

来源:互联网 发布:帝国cms 灵动标签 sql 编辑:程序博客网 时间:2024/05/16 06:12

oracle的技巧有许多,这里只是简单的一些应用,当然了;
这都是咱会滴,不会滴那俺也没什么办法,不是;


SQL的分页语句:
    主要概念是子查询,他拥有两个条件,嵌套的子查询中拥有一个条件,然后外查询中也拥有一个条件;
    注:条件1为广义条件,条件2为狭义条件,取交集;
        select * from (select n.* , 行数 from 表名 n where 条件1 ) where 条件2
    例句:
        select * from (select n.* , rownum r from emp n where rownum <=5) where r>5


SQL 语句中拥有类似于if-else判断的语句,刚知道的时候也挺吓一跳的;
    一.他就是case,第一种语法:
        select 显示字段 , case 条件字段 when 值 then 执行结果 …… else 执行结果 end from 表名;
        java代码表示:
            if(条件字段==值)
                执行结果
            else
                执行结果
    例子:
        select DEPTNO ,CASE DEPTNO
            when 10 then '王'
            when 20 then '哲'
            when 30 then '涵'
        else
             '没有'
        end
        from emp order by DEPTNO asc
   
    显示结果:   
        DEPTNO     CASEDEPTNOWH
        10             王
        10             王
        10             王
        20             哲
        20             哲
        20             哲
        20             哲
        20             哲
        30             涵
        30             涵
        30             涵
        30             涵
        30             涵
        30             涵
   
       
        搜索case 语句:
            第一种方法不错,但是有很大的限制,比如定义case 条件字段时,便只能使用一个字段的判断,这样就产生第二种搜索case
            select 显示字段 , case  when 表达式 then 执行结果 …… else 执行结果 end from 表名;
        例子:
        select DEPTNO ,CASE
            when DEPTNO=10 then '王'
            when empno=20 then '哲'
            when DEPTNO=30 then '涵'
        else
             '没有'
        end
        from emp order by DEPTNO asc
   
    二.decode 也有表达判断的能力,比之case更加的简单;
            语法:select decode(字段,值,满足结果,不满足的结果) from 表名;
            例子:
                select decode(COMM,300,'有','无') from emp
            显示结果:
                DECODE
                无
                有
                无
                无

 
原创粉丝点击