zoukankan      html  css  js  c++  java
  • 06_mysql多表查询

    1 关系数据表的各种操作

    并(union)

    • 就是把具有相同字段数目字段类型的表结合并到一起。

    例如:

    name sex
    tom girl
    jack boy
    name sex
    tom girl
    rose boy

    并的结果

    name sex
    tom girl
    jack boy
    rose boy

    笛卡尔积

    • 笛卡尔积就是没有连接条件表关系返回的结果
    • 注意点就是没有连接条件

    内连接

    • 基于笛卡尔积的数据记录表中,按照相应字段值的比较条件进行选择生成一个新的关系

    分类:

    • 自然连接:

    • 等值连接:

    • 不等连接:

    外链接

    分类:

    • 左外连接
    • 右外连接
    • 全外连接

    2 内连接查询

    3 外链接查询

    4 子查询

    为什么使用子查询?

    • mysql软件虽然可以通过连接查询实现夺标查询数据记录,但是不建议使用,这是
      因为连接查询的性能很差!!因此出现了连接查询的替代者子查询

    • 在具体的开发应用中,mysql软件推荐使用子查询来实现多表查询数据记录

    • 所谓的子查询,就是在一个查询之中嵌套了其它的若干查询,即在一个select查询语句的where或from子句中包含的另一个select查询语句

    注意点

    • 子查询一般出现在where,from子句中

    • 该查询语句中可能包含in,any,all,exists

    • where:该处的子查询一般返回单行单列,多行单列,单行多列数据

    • from:该位置的子查询一般返回多行多列数据记录,可以当做一张临时表

    返回结果是单行单列和单行多列

    • 单行单列
    select * from t_emp where sal > (select sal from t_emp where ename = 'tzhaodi')
    
    • 单行多列(很少出现)
    select ename from where (sal,job) = select sal,job from t_emp where ename = 'zhaodi'
    

    返回结果是多行单列子查询

    当查询的结果是多行多列的时候,该子查询语句一般会在where语句之后出现,通常包含in,any,all,exists

    • in
    select username from t_student where username in (select username from t_class)
    
    • any

    = any,>any,<any

    select * from studnet where stu_age >any(select stu_age from class where stu_name = 'zhaodi')
    
    
    • exists

    • all

    返回结果是多行多列子查询

    一般在from语句之后出现使用

    一个小小的程序员
  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/zhaod/p/8371576.html
Copyright © 2011-2022 走看看