zoukankan      html  css  js  c++  java
  • SQL OUTER JOIN

    When we want to select out all the record from two table, no matter it's present at second table or not, we will have to use SQL OUTER JOIN command.

    There are 3 type of OUTER JOIN, which is:
    LEFT OUTER JOIN
    RIGHT OUTER JOIN
    FULL OUTER JOIN

    SQL OUTER JOIN syntax:

    SELECT *
    FROM [TABLE 1] OUTER JOIN [TABLE 2]
    ON [TABLE 1].[COLUMN NAME 1] = [TABLE 2].[COLUMN NAME 2]


    EXAMPLE :

    Let's say we got 2 tables containt data like Below:

    Table 1: GameScores

    PlayerName DepartmentId Scores
    Jason 1 3000
    Irene 1 1500
    Jane 2 1000
    David 2 2500
    Paul 3 2000
    James 4 2000

    Table 2: Departments

    DepartmentId DepartmentName
    1 IT
    2 Marketing
    3 HR
    5 QA

    LEFT OUTER JOIN will return all records from the first table,
    SQL statement and Result for LEFT OUTER JOIN is :

    SELECT * FROM GameScores2
    LEFT OUTER JOIN Departments
    ON GameScores2.DepartmentId = Departments.DepartmentId

    Result:

    PlayerName DepartmentId Scores DepartmentId DepartmentName
    Jason 1 3000 1 IT
    Irene 1 3000 1 IT
    Jane 2 3000 2 Marketing
    David 2 3000 2 Marketing
    Paul 3 3000 3 HR
    James 4 2000 NULL NULL

    RIGHT OUTER JOIN will return all records from the second table,
    SQL statement and Result for RIGHT OUTER JOIN is :

    SELECT * FROM GameScores2
    RIGHT OUTER JOIN Departments
    ON GameScores2.DepartmentId = Departments.DepartmentId

    Result:

    PlayerName DepartmentId Scores DepartmentId DepartmentName
    Jason 1 3000 1 IT
    Irene 1 3000 1 IT
    Jane 2 3000 2 Marketing
    David 2 3000 2 Marketing
    Paul 3 3000 3 HR
    NULL NULL NULL 5 QA

    FULL OUTER JOIN will return all records from all tables,
    SQL statement and Result for FULL OUTER JOIN is :

    SELECT * FROM GameScores2
    FULL OUTER JOIN Departments
    ON GameScores2.DepartmentId = Departments.DepartmentId

    Result:

    PlayerName DepartmentId Scores DepartmentId DepartmentName
    Jason 1 3000 1 IT
    Irene 1 3000 1 IT
    Jane 2 3000 2 Marketing
    David 2 3000 2 Marketing
    Paul 3 3000 3 HR
    James 4 2000 NULL NULL
    NULL NULL NULL 5 QA
  • 相关阅读:
    dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算
    gdb 调试
    chrome 浏览器命令
    Can't locate Switch.pm in @INC
    Git 图解剖析
    Android STL PORT
    Git 使用及原理 总结
    ndk-gdb 对java/native code联合调试(升级版)
    [转载] 关关采集不能生成html的问题
    TortoiseGit文件夹和文件图标不显示解决方法
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/3327808.html
Copyright © 2011-2022 走看看