我如何通过SQL加入这些表?

时间:2016-12-09 17:50:47

标签: sql

Table1Orders
   OrderID(pk)
   ISBN(fk)
   ProductID(fk)
   OrderDate
   Total

Table2Books
   ISBN(pk)
   AuthorID(fk)
   PublisherID(fk)
   Title
   PubDate

Table3Authors
   AuthorID(pk)
   AuthorName
   AuthorAddress
   AuthorPhone

Table4Publishers
   PublisherID(pk)
   PublisherName
   PubAddress
   PubPhone

我如何找到哪个发布商销售的图书最多?

我需要在INNER JOINOrders详细信息之间Book吗?

我如何对返回的数据进行排序,以便将publisherISBN相关联并且大多数已售出?

1 个答案:

答案 0 :(得分:1)

如果您将图书和图书的订单加入发布商,以便将发布商订购链接作为表格,则根据列名称,然后按发布商,按发布商分组查询并计算行数:

select p.PublisherName, count(*) as BooksSold
from Table1Orders o 
inner join Table2Books b on o.ISBN = b.ISBN
inner join Table4Publishers p on b.PublisherID = p.PublisherID
group by p.PublisherName
order by count(*) desc

Here is a sample with explicit and implicit joins