• Oracle 左连接(+)加号用法及常用语法之间的关系


    本文目的:

    通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法

    分析步骤:

    1.首先创建测试表的结构:

    create table test_left_a (
    a varchar2(50),
    b varchar2(50)
    )
    create table test_left_b (
    a varchar2(50),
    b varchar2(50)
    )

    2.插入相应的测试数据:

    insert into test_left_a select 'a','21' from dual;
    commit;
    insert into test_left_a select 'c','2111' from dual;
    commit;

    insert into test_left_b select 'a','12' from dual;
    commit;
    insert into test_left_b select 'b','13' from dual;
    commit;

    3.列举出实现左连接查询的几种常用的语法,以便对比分析

    实现左连接查询(不加where)的几种语法:
    A: 
    select * from test_left_a a left join test_left_b b on a.a = b.a;

    B: 
    select * from test_left_a a, test_left_b b where a.a = b.a(+);

    C:
    select *
    from test_left_a a
    inner join test_left_b b on a.a = b.a(+);
    实现左连接查询(加where)的几种语法: 
    D:
    select *
    from test_left_a a
    left join test_left_b b on a.a = b.a
    where a.a = b.a;

    E:
    select *
    from test_left_a a, test_left_b b
    where a.a = b.a(+)
    and a.a = b.a;

    F:
    select *
    from test_left_a a
    inner join test_left_b b on a.a = b.a(+)
    and a.a = b.a;
    G:
    select *
    from test_left_a a
    inner join test_left_b b on a.a = b.a(+)
    where a.a = b.a;

    区分where的目的是为了由浅入深,避免在理解类似E写法的时候出现on的误导引起偏差

    4.结论:

    以上几种查询(暂不考虑性能,只考虑用法)
    A等价于B等价于C
    查询结果:
    a 21 a 12
    c 2111
    D等价于E等价于F等价于G
    查询结果:
    a 21 a 12

    5.温馨提示:

    ▶使用inner join的时候 直接在on后面写条件和在where后再写条件是一样的,原因是内连接是匹配出on条件为真的记录(参考F和G)。
    ▶使用left join或者right join的时候,直接在on后面写条件和在where后再写条件是不一样的,原因是:
      left join即使on后面的条件为假也会显示出左表的所有记录
      right join即使on后面的条件为假也会显示出右表的所有记录。

    ▶本文内容如有不妥之处,恳请指正。

  • 相关阅读:
    数学建模(一)层次分析法
    数学建模(二)优劣解距离法Topsis模型部分
    python语法学习第十一天--模块
    机器学习——交叉验证,GridSearchCV,岭回归
    机器学习——logistic回归,鸢尾花数据集预测,数据可视化
    机器学习入门——线性回归预测广告投入数据集
    凸优化,对偶问题与拉格朗日函数
    机器学习中的凸优化,凸集,凸函数的相关定义和理论
    调整数组顺序使奇数位于偶数前面
    leetcode| 329. 矩阵中的最长递增路径
  • 原文地址:https://www.cnblogs.com/LoveShare/p/9774216.html
Copyright © 2020-2023  润新知