zoukankan      html  css  js  c++  java
  • TSQL之JOIN(连接)彻底理解

    T-SQL之JOIN彻底理解

    本文不解释,欢迎提问解答,谢谢。直接上代码:

    --测试数据建立
    Create TABLE JOIN_TEST(
    COLA INT,
    COLB INT
    )
     
    GO
    INSERT INTO JOIN_TEST
    SELECT 1,1
     
    INSERT INTO JOIN_TEST
    SELECT 1,1
     
    GO
    --测试数据建立完毕
     
    SELECT * FROM JOIN_TEST
    /*
     
    COLA        COLB
    ----------- -----------
    1           1
    1           1
     
    (2 行受影响)
    */
     
     
    SELECT * FROM JOIN_TEST A  CROSS JOIN JOIN_TEST B
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
    1           1           1           1
    1           1           1           1
    1           1           1           1
    1           1           1           1
     
    (4 行受影响)
     
    */
     
     
    SELECT * FROM JOIN_TEST A  INNER JOIN  JOIN_TEST B ON A.COLA=B.COLA
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
    1           1           1           1
    1           1           1           1
    1           1           1           1
    1           1           1           1
    (4 行受影响)
     
    */
     
     
    SELECT * FROM JOIN_TEST A  LEFT JOIN  JOIN_TEST B ON A.COLA=B.COLA
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
    1           1           1           1
    1           1           1           1
    1           1           1           1
    1           1           1           1
    (4 行受影响)
     
    */
     
    SELECT * FROM JOIN_TEST A  LEFT JOIN  JOIN_TEST B ON A.COLA=B.COLA AND A.COLA<>1
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
    1           1           NULL        NULL
    1           1           NULL        NULL
    (2 行受影响)
    */
     
    SELECT * FROM JOIN_TEST A  LEFT JOIN  JOIN_TEST B ON A.COLA=B.COLA WHERE A.COLA<>1
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
     
    (0 行受影响)
    */
     
    SELECT * FROM JOIN_TEST A  FULL JOIN  JOIN_TEST B ON A.COLA=B.COLA AND A.COLA<>1
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
    1           1           NULL        NULL
    1           1           NULL        NULL
    NULL        NULL        1           1
    NULL        NULL        1           1
    */
     
    SELECT * FROM JOIN_TEST A  FULL JOIN  JOIN_TEST B ON A.COLA=B.COLA where A.COLA<>1
    /*
    COLA        COLB        COLA        COLB
    ----------- ----------- ----------- -----------
     
    (0 行受影响)
    */
  • 相关阅读:
    PS插件安装
    在linux中安装Python
    快慢指针 | 环形链表
    Intel VT-x 支持但处于禁用状态开启
    函数
    连接(交叉连接、内连接、外连接、自连接)
    游标cursor 与循环fetch
    Identity 自增长标识
    Trigger 触发器
    Procedure 存储过程
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2478551.html
Copyright © 2011-2022 走看看