zoukankan      html  css  js  c++  java
  • 第一章 oracle常用查询语句

    一、简单查询

    1、查询emp表中的全部数据

    select * from emp;

    2、查询emp表中的雇员编号、姓名信息

    select empno,ename from emp;

    3、查询emp中的职位信息,并用distinct去重

    select distinct job from emp;

    4、带计算的查询:查询雇员编号和年薪、日薪

    select empno,ename,sal*12,sal/30  from emp;

    5、为查询结果设置别名

    select empno,ename,sal*12 as 年薪,round(sal/30,2) as 月薪 from emp;

    as可以省略

    select empno 雇员编号,ename 雇员姓名 from emp;

    6、拼接字符串

    select '雇员编号是:'||empno||','||'雇员姓名是:'||ename from emp;

    二、条件查询

    1、查询工资大于5000的员工

    select * from emp where sal>5000;

    2、查询姓名为tom的信息

    select *  from ename='tom';

    3、查询job为clerk的信息

    select * from emp where job='clerk';

    4、查询job不等于clerk的信息

    (1)方式一:

    select * from emp where job<>'CLERK';

    (2)方式二:
    select * from emp where job!='CLERK';

    5、查询工资在5000~10000之间的员工

    select * from emp where sal>=5000 and sal<=10000;

    between 5000 and 10000包括了5000和10000

    select * from emp where sal between 5000 and 10000;

    6、查询出3部门的经理或者5部门的员工

     select * from emp where  (deptno=3 AND job='MANAGER') OR (deptno=5 AND job='CLERK') ;

    7、使用not求反,查询职位不是经理的信息

    select *  from emp where not job='MANAGER'

    8、查询奖金为空的信息

    select * from emp where comm is null;

    9、查询奖金不为空的信息

    select * from emp where comm is  not null;

    10、通过in查询指定范围,查询雇员编号为7499,7521,7566

    select * from emp where empno in(7499,7521,7566);

    这个语句与下面语句相同

    select * from emp where empno='7499' or empno='7521' or empno='7566' ;

    11、查询出雇员姓名以M开头的信息

    select * from emp where ename like 'M%';

    12、查询出雇员姓名的第二字字母是M的雇员信息

    select * from emp where ename like '_M%';

    13、查询出雇员姓名的第三字字母是M的雇员信息,此处两个下滑线,下滑代表任意一个字符

    select * from emp where ename like '__M%';

    14、查询出姓名以H结尾的雇员信息

    select * from emp where ename like '%H';

    15、查询出倒数第二个字符为T的雇员信息

    select * from emp where ename like '%T_';

    16、查询出姓名中任意位置包含H字符的信息

    select * from emp where ename like '%H%';

    17、like中不设置查询关键字查询的是所有信息

    select * from emp where ename like '%%';

    三、查询排序

    1、查询雇员信息并由低到高进行排序

    select * from emp order by sal;

    2、查询雇员信息并按照工资由高到低排序

    select * from emp order by sal desc;

    3、利用序号设置排序列,例如下面的语句是按照sal排序

     select empno,ename,sal,job from emp order by 3 desc;

    4、查询出job为clerk的信息,并按照工资由低到高排序

    select empno,ename,sal,job from emp  where job='CLERK' order by 3 desc;

    5、查询雇员信息,按照工资由高到底,工资相等的按照雇佣时间排序

    select empno,ename,sal,job from emp  where job='CLERK' order by sal desc,hiredate ;

    四、字符串函数

    1、将字符转换为大写

    select  upper('hello world') from dual;

    2、将字符转换为小写

    select  lower('HELLO WORLD') from dual;

    3、将字符串首字母大写

    select  initcap('hello') from dual;

    4、替换字符

    将字符串中所有h字符替换为m

    select  replace('hello','h','m') from dual;

    5、获取字符串长度length(str)

    查询出姓名长度是5的雇员信息

    select * from emp where length(ename)=5;

    6、截取字符串substr

    查询雇员信息,并显示雇员姓名的前三个字母

    select ename as 姓名,substr(ename,0,3)  as 前三个字母 from emp ;

    查询雇员信息,并显示雇员姓名的后三个字母

    select ename as 姓名,substr(ename,length(ename)-2)  as 后三个字母 from emp ;

    7、截取函数时用负数

    截取字符串中的最后三个字母

    select ename as 姓名,substr('hello world',-3)  as 后三个字母 from emp ;

    8、截取的时候可以以0开始也可以以1开始,效果相同

    select ename as 姓名,substr(ename,0,3) as 后三个字母 from emp ;
    select ename as 姓名,substr(ename,1,3) as 后三个字母 from emp ;

    9、查询指定字符的ASCII码

    select ascii('M') from dual;

    10、将ASCII码变回字符——chr()

    select chr(77) from dual;

    11、去掉字符串左边空格——ltrim(str)

    select '      orcl ', ltrim('  orcl ')from dual;

    12、去掉字符串右边空格——rtrim(str)

    select '      orcl ', rtrim('  orcl ')from dual;

    13、去掉字符串两边空格——trim(str)

    select '    hello world  ', trim('    hello world  ')from dual;

    14、字符串左填充——lpad()

    wifi字符串左边填充16个*

    select lpad('wifi',16,'*') from dual;

    15、字符串右填充

    select rpad('wifi',16,'*') from dual;

    16、左右填充组合使用

    SELECT LPAD('wifi' , 10 , '*') 左填充 , RPAD('wifi' , 10 , '*') 右填充 ,
    LPAD(RPAD('wifi' , 10 , '*') , 13 , '*') 组合填充
    FROM dual ;

    17、字符串查找——instr(),可以查找到返回1,不能返回0

    SELECT instr('java web','java'),
    instr('oracle','java')
    FROM dual ;

    五、字符函数

  • 相关阅读:
    前端面试题六
    前端面试题五
    前端面试题四
    前端面试题之三
    前端面试题分享二
    前端面试题分享一
    JS学习笔记一
    git使用学习笔记一
    常见User-Agent
    ado.net之SQLServer和Oracle (sys_cursor) 数据库链接——获取结果集方式对比
  • 原文地址:https://www.cnblogs.com/woshinige/p/15797630.html
Copyright © 2011-2022 走看看