内部连接的 SQL where 子句

Posted

技术标签:

【中文标题】内部连接的 SQL where 子句【英文标题】:SQL where clause on inner joins 【发布时间】:2016-08-17 08:45:24 【问题描述】:

我正在使用 pgadminIII (postgreSQL)

我有以下表格: 书, 出版商, 订单行, ShopOrder。

使用以下 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

.

我想将它与 (WHERE Publisher.Name = "Smith Smitheson") 一起使用

我不知道把它放在哪里,或者如果我做得对,请帮忙?

【问题讨论】:

【参考方案1】:
...
INNER JOIN Publisher ON Book.PublisherID = Publisher.PublisherID AND Publisher.Name = 'Smith Smitheson' )
...

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

【讨论】:

以上是关于内部连接的 SQL where 子句的主要内容,如果未能解决你的问题,请参考以下文章

如何在 mysqli 准备语句中使用多个内部连接和多个 WHERE 子句? [复制]

带有 WHERE 子句的 MySql 内部连接 ​​[关闭]

内部联接或 WHERE 子句中的 AND 语句之间的区别

SQL - 用“where”子句连接三个表

SQL:在 Where 子句之后加入

在 Oracle SQL 中,除了外连接之外,where 子句中的 (+) 运算符的目的是啥?