SELECT *
FROM Orders order by CustomerID
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10308 | 2 | 7 | 1996-09-18 | 3 |
10365 | 3 | 3 | 1996-11-27 | 2 |
10355 | 4 | 6 | 1996-11-15 | 1 |
10383 | 4 | 8 | 1996-12-16 | 3 |
10278 | 5 | 8 | 1996-08-12 | 2 |
SELECT *
FROM Customers
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constituci� 2222 | M�ico D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquer� | Antonio Moreno | Mataderos 2312 | M�ico D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
두개를 LEFT OUTER JOIN 하게 되면
SELECT *
FROM Customers a
LEFT JOIN Orders b
ON a.CustomerID=b.CustomerID
ORDER BY Customers.CustomerName;
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country | OrderID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany | null | null | null | null |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constituci� 2222 | M�ico D.F. | 05021 | Mexico | 10308 | 7 | 1996-09-18 | 3 |
3 | Antonio Moreno Taquer� | Antonio Moreno | Mataderos 2312 | M�ico D.F. | 05023 | Mexico | 10365 | 3 | 1996-11-27 | 2 |
결과에서 보면 LEFT OUTER JOIN 은
ON CustomerID 를 기준으로 Orders 테이블과 일치하는 레코드가 없으면, Orders 테이블의 모든 컬럼이 담긴 row에 NULL을 넣는다
'2019년 이전 정리 > DB_MSSQL' 카테고리의 다른 글
[MS-SQL] 실행계획 Nested Join Merge Join 문제해결 (0) | 2015.02.25 |
---|---|
[SQL] dateadd 문법 (0) | 2014.02.10 |