zoukankan      html  css  js  c++  java
  • 第13章 子查询和集合运算

    第13章 子查询和集合运算
    标量子查询只返回一个值的查询:一行,一列。
    子查询可以将任何常见子句用于投影和选择。
    必须语句
    select
    from
    可选子句
    where
    group by 
    having
    在from子查询中,可以嵌套无限级,在where子句中只能嵌套255级。可以在select from where having中使用子查询。
     
    子查询的结果集用于比较
    select * from emp where salary<(select avg(salary) from emp);
    select department_name from departments where department_id in (select distinct(department_id) from emp);等价于select department_name from departments join emp on departments.department_id=emp.department_id grouo by department_name;
     
    使用not in 会因为SQL处理null的方式而带来问题。除非确定结果集中不含有null.
    子查询可以用来选择要插入的行,但它们不在insert的values子句中。
    适用于单行子查询的比较运算符是:=,>,>=,<=,<>,!=
    适用于多行子查询的是in,not in,any,all
     
    union 合并,排序,删除重复行
    union all合并所有
    intersect 交集 排序,删除重复行
    minus差集 排序 去重
     
    子查询可以相互嵌套,嵌套等级没有限制
    不能在group by和group by 子句中使用子查询。
    复合查询的结果集通常取更高的精度。相关列的必须是相同的数据类型组。
     
    关联子查询
    如果子查询引用父查询中的列,那么其结果就依赖父查询。
    关联子查询必须为外查询中的每一行都计算一次
     
    select name tail_length,to_char(null) from cats
    union all
    select name,to_char(null),wing_span from birds;
    使用to_char(null)生成遗漏的值
    在复合查询中使用order by子句必须是第一个查询中的列的名称(或列别名)

  • 相关阅读:
    python学习永久存储和异常处理
    python学习os文件系统模块
    python学习文件
    python学习集合
    python学习字典
    python学习递归
    python学习函数
    python学习序列
    js加入收藏
    判断dataset和datareader中是否存在某列
  • 原文地址:https://www.cnblogs.com/zhangyuanbo12358/p/9184830.html
Copyright © 2011-2022 走看看