zoukankan      html  css  js  c++  java
  • 简单查询

      

    关键词:

    from:明确数据来源的表

    where:对数据进行条件过滤操作(符合where条件的数据)

    group by :对满足where条件之后的数据进行分组操作。(数据变成组数据)

    having :分组之后的数据进行过滤

    select:选择数据获取的列

    order by :排序

    一、简单查询


     

    • 1.查询数据

         方法1:(查询部分数据)

    select  employee_id,first_name,salary
    
    from employees;

        方法2:(查询所有信息)

    select  *
    
    from employees;

        实际应用中,不会总查询所有信息;方法1可读性高

    • 2.查询结果起别名(查询结果显示的列起名字)

    select 列名1 as 别名,列名2  as 别名
    
    from  表;

        as可以省略。  

    • 3.查询结果的字符串拼接   | |  (相当于java +)

    select first_name||salary
    
    from employees;

        注意:Oracle  表示数据字符串       '字符串'

    • 4.查询结果做算术运算: +   -  * 

      select salary*13
    
      from employees;

        每行数据都会执行算术运算,并将结果展示在查询中

    二、去重


     

      查询结果重复数据,关键字distinct

      语法结构:

      select distinct  列名,列名,列名     from  表;
    
      select  distinct  salary 
    
       from employees;

    三、排序


      关键字:order by 列名  asc或者desc

      备注:asc 升序    desc 降序

      作用:作用在左边的列上

      语法结构:

        select  .....  from  .....order by  排序所依据的字段     asc | desc (排序规则)

    select  empno,sal
    
    from emp
    
    order by sal asc;
    或者
    order
    by sal asc,empno desc;

    四、条件


     

      关键词:where 条件

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

    • 1.等值查询

    select employee_id,first_name,salary
    from employees
    where salary=17000;
    • 2.多条件查询

        where  条件1   or | and   条件2

        关键词:or  或 ,  and  且 

    select employee_id,first_name,salary,department_id
    from employees
    where salary=2500  and department_id=30;
    • 3.不等值查询

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

    select employee_id,first_name,salary
    from employees
    where salary>10000;
    • 4.区间查询

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

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

    方法一:
    select
    employee_id,first_name,salary from employees where salary>5000 and salary<10000;

    方法二:
    select employee_id,first_name,salary
    from employees
    where salary between 5000 and 10000;
    • 5.判断null

        语法:where   字段   is  [not]  null

    1 select employee_id,first_name,salary,commission_pct
    2 from employees
    3 where commission_pct is null;
    • 6.枚举查询

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

        查询60,70,80号部门员工信息

        方法一:

    1 select employee_id,first_name,salary,department_id
    2 from employees
    3 where department_id=60 or department_id=70 or department_id=80;

        方法二:

    1 select employee_id,first_name,salary,department_id
    2 from employees
    3 where department_id in (60,70,80);
    • 7.模糊查询

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

        模糊匹配语法:

        _  :任意1个字符

        % :任意0~n个字符

         查询员工姓以K开头的员工信息:

    1 select employee_id,first_name,last_name,salary
    2 from employees
    3 where    last_name like 'K%';

         查询员工姓长度为4

    1 elect employee_id,first_name,last_name,salary
    2 from employees
    3 where    last_name like '____';
    • 8.特殊关键词

        dual:虚表。一行一列的表

        说明:

        • 1.站在数据的角度,没有意义
        • 2.维护Oracle 的sql语句语法完整性

        sysdate:当前系统时间,精确到秒

        systimestamp:时间戳,精确到毫秒

         查询当前系统时间:

    1 select sysdate
    2 from dual;
    1 select systimestamp
    2 from dual;

     

    五、函数


     

    概念:特定功能的命令

    • 1.单行函数:

         特点:每条查询处理的原数据,产生一条函数处理结果

        常用:

        1.to_char(被转化日期,'日期的格式关键词')

        将日期转化成字符串

    yyyy
    mm
    dd
    hh24  二十四时制
    mi
    ss
    星期 day

        查询当前系统时间:'yyyy-mm-dd'

    1 select to_char(sysdate,'yyyy-mm-dd')
    2 from dual;

        查询员工(工号、名字、薪资、入职日期  yyyy-mm-ddd):

    1 select employee_id,first_name,salary,to_char(hire_date,'yyyy-mm-dd')
    2 from employees; 

        查询今日是星期几

    1 select to_char(sysdate,'day')
    2 from dual;

        2.to_date(被转化的字符串,'日期匹配的内容')

        将字符串转化成日期

        将‘2020-12-31’转化成日期:

    1 select to_date('2020-12-31','yyyy-mm-dd')
    2 from dual;

        查看2020年12月31日是周几

    1 select to_char(to_date('2020-12-31','yyyy-mm-dd'),'day')
    2 from dual;
    • 2.组函数:

        作用:对原表的数据,分组,统计

        特点:每组数据产生1条结果

        常见组函数:

      • max(列):最大值
      • min(列):最小值
      • avg(列):当前组中的数据平均值
      • sum(列):统计总和
      • count(列):统计数量、个数

        组函数对null不作任何统计 

        统计员工个数

    1 select count(employee_id)
    2 from employees;

        统计员工平均工资

    1 select avg(salary)
    2 from employees;

         统计拥有提成的员工个数

    select count(commission_pct)
    from employees;

     

    六、分组


     

      关键词:group by  列名

      作用:对源数据,按照指定的列,进行分组操作

      语法规则:

      • 1.group by 在 where 之后执行
      • 2.select 后可写分组 group by所依据的字段(列)
      • 3.select 可写组函数统计字段

      统计各个部门的平均工资

    1 select avg(salary),department_id
    2 from employees
    3 group by department_id;

     

    七、分组过滤


     

      关键词:having 条件

      作用:对分组之后的组数据进行过滤

    统计平均工资大于8000的部门有哪些

    1 select avg(salary),department_id
    2 from employees
    3 group by department_id
    4 having avg(salary)>8000;

     重点说明

    统计80,90,100部门的总工资

      方法一:使用having过滤

    1 select sum(salary),department_id
    2 from employees
    3 group by department_id
    4 having department_id in(80,90,100);

      方法二:使用where过滤

    1 select sum(salary),department_id
    2 from employees
    3 where department_id in (80,90,100)
    4 group by department_id;

    结论:

    1.where 是在分组之前对数据进行过滤操作

    2.having 是在分组之后,对数据进行操作

    3.优先使用where

  • 相关阅读:
    django表单字段
    python3之Django表单(一)
    python3之Django模型(一)
    python3迭代器和生成器
    python3数字、日期和时间
    python3字符串与文本处理
    python3数据结构与算法
    git仓库使用
    django邮件
    python3光学字符识别模块tesserocr与pytesseract
  • 原文地址:https://www.cnblogs.com/lhl0131/p/12434135.html
Copyright © 2011-2022 走看看