zoukankan      html  css  js  c++  java
  • SQL SERVER LEFT JOIN, INNER JOIN, RIGHT JOIN

    • JOIN: 如果表中有至少一个匹配,则返回行
    • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
    • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
    • FULL JOIN: 只要其中一个表中存在匹配,就返回行
    CREATE TABLE dbo.Student
    (
     Sno int  null
    ,Name nvarchar(23) 
    )
    CREATE TABLE dbo.Score
    (
     Sno int 
    ,Score INT
    )
    INSERT INTO dbo.Student(Sno,Name)VALUES(1,'Jesse');
    INSERT INTO dbo.Student(Sno,Name)VALUES(2,'Jessca');
    INSERT INTO dbo.Student(Sno,Name)VALUES(3,'June');
    INSERT INTO dbo.Student(Sno,Name)VALUES(4,'Supper');
    
    INSERT INTO dbo.Score(Sno,Score)VALUES (1,45)
    INSERT INTO dbo.Score(Sno,Score)VALUES (1,56)
    INSERT INTO dbo.Score(Sno,Score)VALUES (2,100)
    INSERT INTO dbo.Score(Sno,Score)VALUES (3,25)  


     查看数据:

    SELECT * FROM dbo.Student
    SELECT * FROM dbo.Score
    

      

    Left Join 会显示出左边的全部数据,右边如果没有对应的值,系统默认NULL

    SELECT *
    FROM dbo.Student a
    LEFT JOIN dbo.Score b
    ON a.Sno=b.Sno
    

      

    INNER JOIN 显示交集

    SELECT *
    FROM dbo.Student a
    INNER JOIN dbo.Score b
    ON a.Sno=b.Sno
    

      

    right join 右表全部显示,左表没有,默认设置为NULL

    SELECT *
    FROM dbo.Student a
    RIGHT JOIN dbo.Score b
    ON a.Sno=b.Sno
    

      

    FULL JOIN 左右边都显示,如果对方没有值得统一设置NULL

    SELECT *
    FROM dbo.Student a
    FULL JOIN dbo.Score b
    ON a.Sno=b.Sno
    

      

  • 相关阅读:
    Anaconda设置虚拟环境并打包exe
    [转]Anaconda, conda, pyenv, virtualenv的区别
    [闲记]2020-2-13
    集合笔记
    Python_列表(list)
    LeetCode 1711. 大餐计数 做题小结
    LeetCode 242. 有效的字母异位词 做题小结
    GitHub Actions教程 使用selenium自动化
    LeetCode 5641. 卡车上的最大单元数 做题小结
    git 批量删除文件夹和文件
  • 原文地址:https://www.cnblogs.com/Jesse-Li/p/7131624.html
Copyright © 2011-2022 走看看