Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 两个一对多表之间的联接_Sql_Ms Access - Fatal编程技术网

Sql 两个一对多表之间的联接

Sql 两个一对多表之间的联接,sql,ms-access,Sql,Ms Access,我有这两个表,我必须编写此SQL查询(Microsoft Access): 写一个查询,显示每一位售出任何书籍的作者的书名(图书编号)和销售量(订购数量) 我尝试过这个查询,但不起作用 SELECT authors.author_id, Count(orders.book_id) AS CountOfbook_id, Sum(orders.quantity_ordered) AS SumOfquantity_ordered FROM authors LEFT JOIN orders ON

我有这两个表,我必须编写此SQL查询(Microsoft Access):

  • 写一个查询,显示每一位售出任何书籍的作者的书名(图书编号)和销售量(订购数量)

我尝试过这个查询,但不起作用

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;
但它不起作用,因为它计算了从客户订单上取下的同一本书的许多倍。这个怎么样

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;

你试过什么?它产生了什么结果/它与您想要的有什么不同?请阅读并接受答案我编写了我在底部尝试的查询SQL,Microsoft Access我尝试了,但我得到了同一作者两次,请参见:我已修改它