理解sql左外连接

时间:2015-01-15 09:08:03

标签: mysql join left-join

我有以下表格:

Articles:
Nr    Name     Price
1011  DU        10
1012  DA        5
1013  DO        20

Clients
Nr    Name    Street   Zip
123   John     ...      ...
234    Will    ...     ...

Orders
Nr   Client_Nr    Art_Nr    Quantity
1     123          1011       1
2     123          1012       2
3     234          1012       2
4     234          1013       5

如果我想知道每个客户的订单总数,我将不得不使用以下声明:

SELECT Clients.Name, 
    SUM(Orders.Quantity * Article.Price) AS "Total" 
FROM Orders 
LEFT OUTER JOIN Articles 
    ON Orders.Art_Nr = Articles.Nr 
LEFT OUTER JOIN Clients 
    ON Orders.Client_Nr = Clients.Nr 
GROUP BY Clients.Name;

我不明白为什么。当一个表中有行可能未在第二个表中引用时,我认为使用了LOJ。有人可以为我打破这个陈述吗?

为什么我不能这样做:

SELECT clients.name, SUM(orders.quantity * articles.price)
FROM orders
    INNER JOIN articles ON orders.art_nr = articles.nr
    INNER JOIN clients ON orders.client_nr = clients.nr
GROUP BY clients.name;

感谢您的帮助和提示!!

0 个答案:

没有答案