zoukankan      html  css  js  c++  java
  • Oracle学习第三课

    简单查询

    语法结构: select 列名,列名,列名 from 表;

    关键词:

    from: 明确数据来源的表.

    select: 选择数据获取的列.

    1. 查询部分列

      --查询员工表中所有员工的工号,名字,薪资信息?
      步骤: 1. 确定数据来源的表.   from
            2. 选择要获取的数据的列.  select 
      
      select employee_id,first_name,salary
      from employees;
       
    2. 查询所有列

      --查询员工表中所有员工的所有信息?
      方式1:
      select employee_id,first_name,last_name,email,phone_number,manager_id,salary,job_id,department_id
      from employees;
      ​
      方式2:
      select *
      from employees;
      ​
      实际开发应用:
          1. 实际工程项目中,不会总是查询所有信息!!!
          2. 开发角度, 方式1的可读性最好.
    3. 查询结果的列起别名[查询结果显示的列起名字]

      select 列名1 as 别名 ,列名2 from 表;
      --查询员工表中所有员工的工号,名字,薪资信息, 显示查询结果列名工号,名字,薪资
      ​
      select employee_id as 工号,first_name as 名字,salary as 薪资
      from employees;
      ​
      注意: as可以省略.
    4. 查询结果的字符串拼接 || [相当于java中的字符串中的 “+” 拼接]

      --查询员工表中所有员工的工号,姓名(名字+姓),薪资信息?
      ​
      select employee_id,first_name || last_name,salary
      from employees;
      ​
      注意:Oracle 表示数据字符串使用单引号 ' 字符串 '
    5. 查询结果做算术运算: + - * /

      --查询员工工号,名字,年薪?
      select employee_id,first_name,salary*13
      from employees;
      ​
      --解释: oracle执行过程中,每行数据都要执行算术运算,将结果展示在查询结果中.

    去重

    查询结果重复数据: 查询结果中,有两行数据完全一样.

    去重: 关键词 distinct

    语法结构: select distinct 列名,列名,列名 from 表

    --查询员工表中,所有领导的工号信息?
    select distinct manager_id
    from employees;

    排序

    关键词: order by 列名1 asc|desc, 列名2 asc|desc

    备注: asc: 升序

    ​ desc: 降序

    ​ 作用: 作用在左边列上.

    语法结构: select .... from ... order by 排序所依据的字段 asc|desc{排序规则}

    -- 查询员工工号,名字,薪资信息,按照salary 升序排序展示?
    select employee_id,first_name,salary
    from employees 
    order by salary asc;
    ​
    -- 查询员工工号,名字,薪资信息,按照salary升序排序,如果salary一样,按照工号降序展示?
    select employee_id,first_name,salary
    from employees 
    order by salary asc,employee_id desc;
    
     

    条件查询

    关键词: where 条件

    语法结构: select.... from ... where 条件 order by 排序字段 desc|asc;

    作用: 对每个查询的数据进行条件判断,将符合条件的存放入查询的结果中.

    1. 等值查询
      
      -- 查询薪资为17000的员工工号,名字,薪资信息?
      select employee_id,first_name,salary
      from employees
      where salary=17000;
    2. 多条件

      where 条件1 or|and 条件2

      关键词: or: 或

      ​ and: 且

      --查询薪资为2500,且部门编号为30的员工信息(工号,名字,薪资,部门id)
      select employee_id,first_name,salary,department_id
      from employees
      where salary=2500 and department_id=30;
      ​
    3. 不等值查询

      逻辑判断符号: > < >= <= !=[<>]

      --查询薪资大于10000的员工信息(工号,名字,薪资)
      select employee_id,first_name,salary
      from employees
      where salary>10000;
    4. 区间查询

      关键词: 判断字段 between 起始值 and 结束值.

      特点: 闭区间 {字段>=起始值 and 字段<=结束值}

      --查询薪资在10000到12000之间的员工信息(员工工号,名字,薪资)
      方式1:
      select employee_id,first_name,salary
      from employees
      where salary>10000 and salary<12000;
      ​
      --查询薪资在10000到12000之间的员工信息,包含10000和12000薪资信息(员工工号,名字,薪资)
      方式2:
      select employee_id,first_name,salary
      from employees
      where salary between 10000 and 12000;   
    5. null

      语法: where 字段 is [not] null;

      --将员工表中,没有提成(commission_pct)的员工薪资展示(工号,名字,薪资,佣金比率);
      select employee_id,first_name,salary,commission_pct
      from employees
      where commission_pct is null;
    6. 枚举查询

      关键词: 字段(列名) in (值1,值2,值3);

      作用:

      --查询60,70,80号部门的员工信息(工号,名字,薪资,部门编号)?
      方法1: 
      select employee_id,first_name,salary,department_id
      from employees
      where department_id = 60 or  department_id = 70 or  department_id = 80;
      ​
      方法2:
      select employee_id,first_name,salary,department_id
      from employees
      where department_id in (60,70,80);
    7. 模糊查询

      关键词: where 列名 like '模糊匹配语法';

      模糊匹配语法:

      _ : 任意1个字符

      %: 任意0~n个字符

      -- 查询员工中姓以K开头的员工信息(工号,名字,姓,薪资)?
      select employee_id,first_name,last_name,salary
      from employees
      where last_name like 'K%';
      ​
      -- 查询员工的姓长度为4(姓是4个字母)的员工信息?
      select employee_id,first_name,last_name,salary
      from employees
      where last_name like '____';

    特殊关键词

    dual: 虚表. 一行一列的表.

    说明:

    1. 站在数据的角度,没有意义.

    2. 维护Oracle的sql语句语法完成性.

    sysdate: 当前系统时间

    ​ 时间: 年 月 日 时 分 秒

    systimestamp: [时间戳] 当前系统时间

    ​ 时间: 年 月 日 时 分 秒 毫秒

    --查询当前系统时间?
    select sysdate from dual;

  • 相关阅读:
    ViewPager+Fragmrnt最简单结合方法
    Microsoft SQL Server Version List(SQL Server 版本)
    hdu 2795 Billboard(线段树单点更新)
    面向对象程序设计的思想的长处
    iOS 友盟分享
    使用Broadcast实现android组件之间的通信
    jquery ui 分页插件 传入后台的连个參数名
    android adb常见问题的解决方法!
    UVa 11015
    优秀程序猿学习方法
  • 原文地址:https://www.cnblogs.com/chenpeisong/p/9751542.html
Copyright © 2011-2022 走看看