• Oracle学习笔记<2>


    1.排序问题

    默认排序规则:按照物理存储顺序。
    指定排序规则:order by关键字
    位置:出现在select语句的最后边。
    语法:select ...
    from ...
    order by 字段 排序规则;
    字段用来定义要根据哪个字段进行排序。
    排序规则:
    1)升序排列 从小到大 ASC
    2)降序排列 从大到小 DESC

    例如:
    查询所有员工的id、salary
    要求按照工资的降序排列?
    select id,salary
    from s_emp
    order by salary desc;

    指定多个排序规则:
    select ...
    from ....
    order by 字段1 排序规则1,
    字段2 排序规则2,
    字段3 排序规则3.....;
    例如:
    查询所有员工的id、salary
    按照工资的降序和id的升序进行排列?
    select id,salary
    from s_emp
    order by salary desc,id asc;

    commission_pct 存在空值
    空值的处理:
    排序过程中,空值视为无限大。
    在升序排列中,空值应该排在最后。
    在降序排列中,空值应该排在最前。

    2.限定性查询/条件查询
    在查询时要根据指定条件
    筛除掉一部分不需要的数据。
    关键字:where
    位置:from子句后面
    语法:select ...
    from ...
    where 判断条件;
    1)等值判断和不等值判断
    字段值等于/不等于某个特定的值。
    例如:
    查询id为1的员工id、last_name、salary?
    select id,last_name,salary
    from s_emp
    where id = 1;

    查询id不为1的员工id、last_name、salary?
    select id,last_name,salary
    from s_emp
    where id != 1;

    不等于:
    写法一:!=
    写法二:^=
    写法三:<>

    空值判断:
    例如:
    查询员工表中所有不拿提成的员工
    id、last_name、salary?
    select id,last_name,salary
    from s_emp
    where commission_pct is null;

    查询员工表中所有拿提成的员工
    id、last_name、salary?
    select id,last_name,salary
    from s_emp
    where commission_pct is not null;

    2)范围判断
    大于 >
    小于 <
    大于等于 >=
    小于等于 <=
    练习:
    查询所有工资高于1100元的员工id、
    last_name、salary?
    select id,last_name,salary
    from s_emp
    where salary > 1100;

    查询所有工资不低于1100元的员工id、
    last_name、salary?
    select id,last_name,salary
    from s_emp
    where salary >= 1100;

    3)条件并列
    a)逻辑与 and
    使用and连接的所有条件必须同时满足
    才会被查询出来。
    b)逻辑或 or
    使用or连接的所有条件只需要满足
    其中一个,就会被查询出来。

    练习:
    查询id在10以内并且工资高于1100元的
    员工id、last_name、salary?
    select id,last_name,salary
    from s_emp
    where id <= 10 and salary > 1100;

    查询41和42号部门所有员工的
    id、last_name、dept_id?
    select id,last_name,dept_id
    from s_emp
    where dept_id = 41 or dept_id = 42;


    and的优先级大于or的优先级。

    查询所有工资高于1100元并且任职在
    41或42号部门的员工信息?
    select id,last_name,dept_id
    from s_emp
    where salary > 1100
    and (dept_id = 41 or dept_id = 42);

    4)逻辑比较符
    between 在给定的最小值和最大值范围之间
    语法:between 较小值 and 较大值
    代表大于等于较小值和
    小于等于较大值的结果会被查询出来。
    注意:一定要先写较小值,再写较大值。

    select id,last_name
    from s_emp
    where salary>=1100 and salary<=1200;
    等同于
    where salary between 1100 and 1200;

    in 在给定的可选值中选一个
    语法:where 字段值 in(值1,值2,值3...);

    练习:
    查询id为1、3、5、7、9的员工信息?
    select id,salary,dept_id
    from s_emp
    where id = 1 or id = 3 or id =5
    or id=7 or id=9;

    select id,salary,dept_id
    from s_emp
    where id in(1,3,5,7,9);

    查询工资不在1100-1200范围内的员工信息?
    select id,last_name,salary
    from s_emp
    where salary not between 1100 and 1200;
    等同于:
    where salary>1200 and salary <1100;

    查询41、42号部门以外的所有员工信息?
    select id,last_name,dept_id
    from s_emp
    where dept_id!=41 and dept_id!=42;

    select id,last_name,dept_id
    from s_emp
    where dept_id not in(41,42);

    3.模糊查询/关键字查询
    语法:where 字段值 like 模糊值;

    通配符:
    1)% 百分号代表任意数量任意字符
    可以没有 可以有一个 可以有多个
    2)_ 下划线代表一个任意字符 --占位符
    有且只有一个任意字符

    例如:
    查询所有last_name中包含'N'的员工信息?
    select id,last_name
    from s_emp
    where last_name like '%N%';

    查询所有last_name第二位是'e'的员工信息?
    select id,last_name
    from s_emp
    where last_name like '_e%';

    例如:
    先向s_emp表中插入一条数据:
    insert into s_emp(id,last_name)
    values(999,'_briup');
    commit;

    查询员工表中last_name以'_'下划线开头的用户信息?
    select id,last_name
    from s_emp
    where last_name like '_%';

    字符转义:
    1)在要转义的字符前面加上一个标识字符
    标识字符可以是任意字符。
    2)使用escape关键字声明哪一个字符是标识字符
    select id,last_name
    from s_emp
    where last_name like 'a_%' escape 'a';

    select id,last_name
    from s_emp
    where last_name like '/_%' escape '/';

  • 相关阅读:
    关于Jupyter Notebook默认起始目录设置无效的解决方法
    关于二叉树中度为0与度为2节点数关系证明
    LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
    数据结构,物理结构,存储结构,逻辑结构的区分
    条件变量 sync.Cond
    defer 的常用场景
    switch...case... 语句中的类型转换
    golang 切片和数组在for...range中的区别
    golang 并发顺序输出数字
    Golang 中哪些值是不可以寻址的
  • 原文地址:https://www.cnblogs.com/weixinyu98/p/9942440.html
Copyright © 2020-2023  润新知