详解ORACLE 11g select

ORACLE 11g select 语法完整描述:

SELECT [ALL|DISTINCT] TOP n[PERCENT] WITH TIES select_list

[INTO[new table name]]

FROME{tabel_name|view_name}[{optimizer_hints}]

[,{table_name2|view_name2}[(optimizer_hints)]]

[...,table_name16|view_name16][(optimizer hints)]]]

[WHERE clause]

[GROUP BY clause]

[HAVING clause]

[ORDER BY clause]

[COMPUTE clause]

[FOR BROWSE]

1、双引号能保持格式
    如:select sysdate “toDay 日 期” from dual;
 
2、|| 字符串连接
    如: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;
    用两个单引号表示一个单引号
 
3、去掉重复数据distinct
    select distinct deptno from scott.emp;
    去掉重复组合:select distinct deptno,job from scott.emp;
 
4、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、innot 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 nullis 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;
    
5、function
    A、lowerupper、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;
 
6、group function 组函数:minmaxavgsumcount
        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;
 
7、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);
 
8、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;
 
9、子查询
    求部门分组后工资最高的员工信息
    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
 
10、自连接
    select a.ename, b.ename mgr_name from emp a, emp b where a.empno = b.mgr;
 
11、 连接查询
    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;
 
12、 Rownum
    select rounum, deptno, dname from dept;
    select * from (
         select rownum r, dept.* from dept
    ) t where  t.r > 2;
 
13、树状结构查询
    select level, empno, ename, mgr from emp
    connect by prior mgr = empno;
 
14、排序函数
    --按部门分组,给出分组后的序号
    select row_number() over(partition by deptno order by sal), emp.* from emp;
    
    --rank排序,空出相同部分
    select rank() over(partition by deptno order by sal), emp.* from emp;
    select rank() over(order by deptno), emp.* from emp;
    select rank() over(order by sal), emp.* from emp;
    
    --dense_rank排序给出相同序号,不空留序号
    select rank() over(order by sal), emp.* from emp;
    select dense_rank() over(order by sal), emp.* from emp;
 
15、交集、并集、割集查询
    --并集:不带重复数据
    select * from emp
    union
    select * from emp2;
    
    --并集:带重复数据
    select * from emp
    union all
    select * from emp2;        
    
    --割集,显示不同部分
    select * from emp
    minus
    select * from emp2;
 
16、 查询系统表、视图
    select owner, object_name, object_type, status, dba_objects.* from dba_objects where object_type = 'view' and status = 'invalid';
 
    select * from user_objects where object_type like 'PROCEDURE';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值