postgresql 内连接的 SQL where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36815513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:15:11  来源:igfitidea点击:

SQL where clause on inner joins

postgresqlinner-joinwhere

提问by Edward Savage

I am working with pgadminIII (postgreSQL)

我正在使用 pgadminIII (postgreSQL)

I have the following tables: Book, Publisher, OrderLine, ShopOrder.

我有以下表格:Book、Publisher、OrderLine、ShopOrder。

With the following SQL:

使用以下 SQL:

SELECT 
    Book.BookID AS "Book ID"
    ,Book.Title AS "Book title"
    ,SUM(OrderLine.quantity) AS "Number Ordered"
    ,ShopOrder.OrderDate AS "Order Date"
FROM (((Book

INNER JOIN OrderLine
ON Book.BookID = OrderLine.BookID)

INNER JOIN Publisher
ON Book.PublisherID = Publisher.PublisherID )


INNER JOIN ShopOrder
ON OrderLine.ShopOrderID = ShopOrder.ShopOrderID)

GROUP BY ShopOrder.OrderDate, Name, Book.BookID

.

I would like to use this with a (WHERE Publisher.Name = "Smith Smitheson")

我想用 ( WHERE Publisher.Name = "Smith Smitheson")

I dont know where to put it, or if im doing it right, any help please?

我不知道把它放在哪里,或者如果我做得对,有什么帮助吗?

回答by George Poliovei

...
INNER JOIN Publisher ON Book.PublisherID = Publisher.PublisherID AND Publisher.Name = 'Smith Smitheson' )
...

or

或者

SELECT Book.BookID AS "Book ID" ,Book.Title AS "Book title"   
,SUM(OrderLine.quantity) AS "Number Ordered" ,ShopOrder.OrderDate AS  
"Order Date" FROM Book
INNER JOIN OrderLine ON Book.BookID = OrderLine.BookID
INNER JOIN Publisher ON Book.PublisherID = Publisher.PublisherID 
INNER JOIN ShopOrder ON OrderLine.ShopOrderID = ShopOrder.ShopOrderID
WHERE Publisher.Name = 'Smith Smitheson'
GROUP BY ShopOrder.OrderDate, Name, Book.BookID