您的位置:首页 > 博客中心 > 数据库 >

Oracle笔记 三、function 、select

时间:2022-03-14 15:18

Scott表下有这么几个常用的表,而且还带有数据。分别是emp、dept、salgrade;
1、查看表结构用desc
    desc emp;
 
2、空表dual,最常用的空表,如:
    select 2 * 4 from dual;
    select sysdate from dual;
 
3、双引号能保持格式
    如:select sysdate “toDay 日 期” from dual;
 
4、|| 字符串连接
    如:select 2*3 || 8 from dual;
    select ename || sal from scott.emp;
    select ename || ‘ORACLE’ from scott.emp;
 
5、单引号,如:select 2 * 2 || ‘abc‘‘efg‘ from dual;
    用两个单引号表示一个单引号
 
6、去掉重复数据distinct
    select distinct deptno from scott.emp;
    去掉重复组合:select distinct deptno,job from scott.emp;
 
7、where查询
    A、=查询,select * from scott.emp where sal = 1500;
 
    B、比较<、>、>=、<=
        select * from scott.emp where sal > 1500;
    C、and or
        select * from scott.emp where sal > 1500 and sal <= 5000 or deptno = 10;
    D、in、not in
        select * from scott.emp where sal in (1500, 800) and deptno not in (10, 20)
 
    E、like模糊 escape 转义
        Select * from scott.emp where ename like ‘%in%’;
        Select * from scott.emp where ename like ‘%in\%k%’;
        Select * from scott.emp where ename like ‘%in#%k%’ escape ‘#’;
        表示like中的#号是转义字符,相当于\
    F、is null、is not null
    K、    order by
        select sal, ename from scott.emp order by sal;
        select sal, ename from scott.emp order by sal asc;
        select sal, ename from scott.emp order by sal desc;
        select sal, ename from scott.emp where sal > 2000 order by sal desc;
        select sal, deptno, ename from scott.emp order by sal,deptno desc;
    
8、function
    A、lower、upper、substr
        select lower(‘abcABC’) from dual;
        select upper(‘abcABC’) from dual;
        substr(target, startIndex, length)
        select substr(‘abcABC’, 1, 3) from dual;
 
    B、chr、ascii
        将数字安装ascii值转换成字符:select char(65) from dual;
        将字符转换成ascii值:select ascii(‘Z’) from dual;
    
    C、round、to_char
        精确小数
        select round(22.456) from dual;
        保留2位小数:select round(22.456, 2) from dual;
        精确到个位:select round(22.456, -1) from dual;
    
        货币
        设置货币格式,000前面不足就用0代替
        select to_char(sal, ‘$000,000.00‘) from scott.emp;
        999就不会替换不足的地方,只会安装格式输出
        select to_char(sal, ‘$999,999.99‘) from scott.emp;
        本地货币格式
        select to_char(sal, ‘L999,999.99‘) from scott.emp;
 
        日期
        日期格式 
        格式控制 描述 
        YYYY、YYY、YY 分别代表4位、3位、2位的数字年 
        YEAR 年的拼写 
        MM 数字月 
        MONTH 月的全拼 
        MON 月的缩写 
        DD 数字日 
        DAY 星期的全拼 
        DY 星期的缩写 
        AM 表示上午或者下午 
        HH24、HH12 12小时制或24小时制 
        MI 分钟 
        SS 秒钟 
        SP 数字的拼写 
        TH 数字的序数词 
 
        “特殊字符” 假如特殊字符 
        HH24:MI:SS AM 15:43:20 PM
        select to_char(sysdate, ‘YYYY-MM-DD HH:MI:SS‘) from dual;
        select to_char(sysdate, ‘YYYY-MM-DD HH24:MI:SS‘) from dual;
 
     D、to_date、to_number、nvl
        to_date(target, current_format)
        select to_date(‘2011-4-2 17:55:55‘, ‘YYYY-MM-DD HH:MI:SS‘) from dual;
        select to_number(‘$12,322.56‘, ‘$999,999.99‘) + 10 from dual;
        select to_number(‘$12,322.56‘, ‘$00,000.00‘) + 10 from dual;
        select to_number(‘22.56‘) + 10 from dual;
        nvl可以将某个字段的空值转换成指定的值
        select ename, sal, nvl(comm, 1.00) from scott.emp;
 
9、group function 组函数:min、max、avg、sum、count
        select max(sal) from scott.emp;
        select min(sal) from scott.emp;
        select avg(sal) from emp;
        select round(avg(sal), 2) from emp;
        select to_char(avg(sal), ‘L999,999.99‘) from emp;
        select sum(sal) from emp;
        select count(comm) from emp;
        select count(distinct deptno) from emp;
 
10、group by 分组
    select deptno, avg(sal) from emp group by deptno;
    select deptno, job, avg(sal) from emp group by deptno, job;
    求部门最高工资的所在部门的员工信息:
    select deptno, ename, sal from emp where sal in (select max(sal) from emp group by deptno);
 
11、having 对分组数据进行过滤
    求部门评价工资:
    select * from (select avg(sal) sal, deptno from emp group by deptno) where sal > 2000;
    select avg(sal) sal, deptno from emp group by deptno having avg(sal) > 2000;
 
12、子查询
    求部门分组后工资最高的员工信息
    select emp.ename, emp.sal, emp.deptno from emp, (select max(sal) max_sal, deptno from emp group by deptno) t where emp.sal = t.max_sal and emp.deptno = t.deptno;
    求部门平均工资等级
    select s.grade, t.deptno, t.avg_sal from scott.salgrade s, (select deptno, avg(sal) avg_sal from emp group by deptno) t where t.avg_sal > s.losal and t.avg_sal < s.hisal;(between)
 
13、自连接
    select a.ename, b.ename mgr_name from emp a, emp b where a.empno = b.mgr;
 
14、 连接查询
    select dname, ename from dept, emp where dept.deptno = emp.deptno;
    select dname, ename from dept join emp on dept.deptno = emp.deptno;
    select dname, ename from dept join emp using(deptno);
    select dname, ename from dept left join emp on dept.deptno = emp.deptno;
    select dname, ename from dept right join emp on dept.deptno = emp.deptno;
    select dname, ename from dept full join emp on dept.deptno = emp.deptno;
    select a.ename, b.ename mgr_name from emp a join emp b on a.mgr = b.empno;
    select a.ename, b.ename mgr_name from emp a left join emp b on a.mgr = b.empno;
 
                            
                        

热门排行

今日推荐

热门手游