zoukankan      html  css  js  c++  java
  • SQL中关于Left Join转为Inner Join的问题,即左关联转为内关联的问题

    先说结论

    所谓实践出真知,个人也是因为对 Left Join 转为 Inner Join 的问题有疑问,于是逐一尝试,最后得出结论。
    从下面的实验结果很容易看出,在有左关联的查询语句中,只要有 WHERE 的过滤条件,那么该语句将被强制转为内关联。

    一、创建表

    演示使用,随便创建两张表

    --班级表
    CREATE TABLE T_CLASS(
      class_id NUMBER not null,
      class_name VARCHAR2(100)
    )
    --学生表
    CREATE TABLE T_STUDENT(
      student_id NUMBER not null,
      class_id NUMBER not null,
      student_name VARCHAR2(100),
      age NUMBER,
      sex CHAR(1) 
    )
    

    二、创建数据

    --班级数据
    insert into T_CLASS (CLASS_ID, CLASS_NAME)
    values (1, '一班');
    
    insert into T_CLASS (CLASS_ID, CLASS_NAME)
    values (2, '二班');
    
    insert into T_CLASS (CLASS_ID, CLASS_NAME)
    values (3, '三班');
    
    insert into T_CLASS (CLASS_ID, CLASS_NAME)
    values (4, '四班');
    
    insert into T_CLASS (CLASS_ID, CLASS_NAME)
    values (5, '五班');
    
    --学生数据
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (1, 1, '李1', 3, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (2, 1, '李2', 2, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (3, 1, '李3', 3, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (4, 2, '李4', 4, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (5, 2, '李5', 3, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (6, 2, '李6', 3, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (7, 3, '李7', 6, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (8, 3, '李8', 4, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (9, 2, '李9', 2, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (10, 2, '李10', 3, '1');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (11, 3, '李11', 3, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (12, 2, '李12', 8, '2');
    
    insert into T_STUDENT (STUDENT_ID, CLASS_ID, STUDENT_NAME, AGE, SEX)
    values (13, 1, '李13', 6, '2');
    

    三、左关联出现的几种情形

    1.无过滤条件

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    2.A表过滤条件在AND中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    3.A表过滤条件在WHERE中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    

    查询结果:
    ##图片

    4.B表过滤条件在AND中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    5.B表过滤条件在WHERE中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE B.AGE = 3
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    6.A表过滤条件在AND中,B表过滤条件在WHERE中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND A.CLASS_ID = 1
     WHERE B.AGE = 3
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    7.A表过滤条件在WHERE中,B表过滤条件在AND中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
     WHERE A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    

    查询结果:
    在这里插入图片描述

    8.A表和B表过滤条件都在AND中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    

    查询结果:
    ##图片

    9.A表和B表过滤条件都在WHERE中

    查询语句:

    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE B.AGE = 3
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    

    查询结果:
    ##图片
    以下附上所有查询语句:

    --1.无过滤条件
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     ORDER BY A.CLASS_ID;
    --2.A表过滤条件在AND中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    --3.A表过滤条件在WHERE中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    --4.B表过滤条件在AND中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
     ORDER BY A.CLASS_ID;
    --5.B表过滤条件在WHERE中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE B.AGE = 3
     ORDER BY A.CLASS_ID;
    --6.A表过滤条件在AND中,B表过滤条件在WHERE中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND A.CLASS_ID = 1
     WHERE B.AGE = 3
     ORDER BY A.CLASS_ID;
    --7.A表过滤条件在WHERE中,B表过滤条件在AND中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
     WHERE A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    --8.A表和B表过滤条件都在AND中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
       AND B.AGE = 3
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    --9.A表和B表过滤条件都在WHERE中
    SELECT *
      FROM T_CLASS A
      LEFT JOIN T_STUDENT B
        ON A.CLASS_ID = B.CLASS_ID
     WHERE B.AGE = 3
       AND A.CLASS_ID = 1
     ORDER BY A.CLASS_ID;
    
  • 相关阅读:
    Windows Azure 架构指南 – 第 1卷 发布
    SQL Azure 入门教学(一):SQL Azure之初体验
    WPC大会新动态: Windows Azure Platform Appliance发布
    Windows Azure AppFabric 入门教学(七):多播(Multicast)
    PHP on Windows Azure 入门教学系列(一):在Windows Azure内运行PHP应用
    SQL Azure SU3 现已在全球6座数据中心开始启用
    WPC大会新动态:合作伙伴采纳Windows Azure
    时间的运算
    把字符串复制到剪贴板
    常用的表格效果
  • 原文地址:https://www.cnblogs.com/zhenggc/p/13655500.html
Copyright © 2011-2022 走看看