T-SQL의 JOIN(연결) 철저한 이해(개발자 필수)

2818 단어 t-sql
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     )
*/

좋은 웹페이지 즐겨찾기