加入两个一对多表

问题描述:

我有这两个表,我必须写这个SQL查询(Microsoft Access中)之间:加入两个一对多表

  • 撰写出了每个作者的销量任何书籍有多少头衔查询(book_id)和多少个单位(订购的数量),他们卖

enter image description here

我试图与该查询,但它不工作

SELECT authors.author_id, Count(orders.book_id) AS CountOfbook_id, 
Sum(orders.quantity_ordered) AS SumOfquantity_ordered 
FROM authors 
LEFT JOIN orders ON authors.book_id = orders.book_id 
GROUP BY authors.author_id; 

,但它不工作,因为它计算同一本书多次从customersOrders enter image description here

+0

那么你试过了什么?它产生了什么结果/它与你想要的有什么不同? –

+0

请阅读http://meta.*.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –

+0

我写了查询我试过底部 – sparkle

这个怎么样了?

select author,sum(CountOfbook_id), sum(SumOfquantity_ordered) 
from (
    SELECT authors.author_id as author, Count(orders.book_id) AS CountOfbook_id, 
    Sum(orders.quantity_ordered) AS SumOfquantity_ordered 
    FROM authors 
    LEFT JOIN orders ON authors.book_id = orders.book_id 
    GROUP BY authors.author_id,authors.book_id;) 
group by author; 
+0

我试过了,但是我得到了两次相同的作者,请看:https://d2ppvlu71ri8gs.cloudfront.net/items/3W2B1G2t232C1p0i3J3f/Image%202017-06-02 %20at%2011.51.59%20 AM.png?v = 59ceaca9 – sparkle

+0

我修改了它 – nacho