Вопрос

I'm having a little confusion with a certain example.

I am supposed to list all orders and their corresponding details.

This is what I'm doing:

SELECT *
    FROM Orders 
    LEFT OUTER JOIN [Order Details]
    ON Orders.OrderID=[Order Details].OrderID;

This gives the number of rows = 2155.

Now the problem is, the number of rows in Orders Table is 830...how can left outer join create more rows ?

By definition of left outer join, all the rows from the left table are taken and matching records from the second table are added.

I checked the number of rows in the Order Details table..that is 2155.

Why is left outer join using all rows from Order Details table?

Это было полезно?

Решение

LEFT JOIN takes all the details from the table you define on the left side of the join and match records from the right table.

If there's no match, all columns of the right table have NULL values.

If there's a match, all matching records from the right table are returned. If your relationship is 1-to-many (as in your case), it means that there may be more than one record returned from the right table for each record in the left table.

Другие советы

LEFT OUTER JOIN will match all records in the right table, just as an INNER JOIN will. The difference is a LEFT JOIN will preserve records from the left table with no match in the right table.

In this scenario, all records in [ORDER DETAILS] have a corresponding entry in ORDERS, which is why the total number of records matches the number of rows in ORDER DETAILS

Based on the table descriptions, this is exactly what you want. Having an ORDER DETAIL without an ORDER would be a much more serious issue.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top