这是我的SQL Server ERD: https://c2.staticflickr.com/6/5832/23786188186_d6f1d93132_o.jpg
我需要找出哪些图书与每个发布商相关联。
USE BookStoreDB
SELECT ProductID
FROM Books
INNER JOIN [Publishers] PublisherID ON PublishersID = ProductID
我假设我没有正确创建INNER JOIN命令?
答案 0 :(得分:0)
SELECT p.PublisherID, b.ProductID
FROM Books b
INNER JOIN Publishers p ON p.PublisherID = b.PublisherID
答案 1 :(得分:0)
连接的ON部分定义了两个表如何相互关联。在你的情况下,它将是:
INNER JOIN Publishers ON (Books.PublisherID = Publishers.PublisherID)
如果在表的名称和ON之间放置了某些内容,则将其视为别名。您可以(也应该)在SELECT
中使用这些别名FROM Books B
INNER JOIN Publishers P ON (B.PublisherID = P.PublisherID)
最后,您可能希望选择实际上会告诉您发布者是谁的信息:
SELECT B.ISBN, P.CompanyName
FROM Books B
INNER JOIN Publishers P ON (B.PublisherID = P.PublisherID)