在 SQL 查询中连接多个表
joining multiple tables in an SQL query
我正在尝试从我的数据库中的多个 table 中获取 select 信息,这些信息显示多本书和订单的详细信息。
我正在将某本书的 isbn 查询到 5 tables(作者、书作者、书、订单行和书单),以检索有关该书的信息和已为该书下的订单的信息书。
SELECT orderline.isbn, title, ordernumber, orderdate, customername, numcopies, orderline.bookprice, authorname
FROM author natural join bookauthor natural join book join orderline natural join bookorder
WHERE orderline.isbn = book.isbn
and book.isbn = "1491936169"
这给了我:
ISBN: 1491936169 Title: Kafka: The Definitive Guide: Real-Time Data and Stream Processing at Scale
Author: Neha Narkhede, Todd Palino, Gwen Shapira
Order Number Date Customer Copies Price Total
N201699998 2016-12-24 Mary Hall 2 33.99 67.98
N201799999 2017-01-03 Aran Clauson 1 33.99 33.99
Total:
然而,有些 isbn 尚未订购,因此不在订单行 table 中,但在书本 table 中,它显示了所有书的 isbn。
我想显示那些没有订单的书的信息,例如:
ISBN: 0387848576 Title: The Elements of Statistical Learning
Author: Jerome Friedman, Trevor Hastie, Robert Tibshirani
No orders
本质上我想要 table 在没有订单的情况下显示 isbn 和 null 值的图书信息。我想这将是某种自然的外部连接,但是我的尝试导致空 tables.
已更新查询尝试使用分组依据
删除重复项
SELECT ordernumber, orderdate, customername, orderline.isbn, title, orderline.numcopies, stock, shipmentbook.numcopies as shipcopies, authorname
FROM author natural join bookauthor natural join book left join bookorder natural join orderline
ON book.isbn = orderline.isbn
left join mousavs.shipmentbook
ON book.isbn = shipmentbook.isbn
WHERE stock > orderline.numcopies
GROUP BY ordernumber
ORDER BY orderdate, ordernumber, ISBN
错误代码:1055。SELECT 列表的表达式 #4 不在 GROUP BY 子句中并且包含非聚合列 'mousavs.orderline.isbn',它在功能上不依赖于 GROUP BY 子句[=17= 中的列]
Left join 101...你真的......如果你打算在 RDMS 中工作,你真的需要阅读有关联接的内容。
select
b.isbn
,b.title
,b.bookprice
,b.stock
,a.authorname
,o.ordernumber
,o.numcopies
,o.price
from
book b
inner join
BookAuthor ba on
ba.isbn = b.isbn
inner join
Author a on
a.authorid = ba.authorid
left join
orderline o on
o.isbn = b.isbn
left join
bookorder bo on
bo.ordernumber = o.ordernumber
where
b.isbn = 1491936169
我正在尝试从我的数据库中的多个 table 中获取 select 信息,这些信息显示多本书和订单的详细信息。
我正在将某本书的 isbn 查询到 5 tables(作者、书作者、书、订单行和书单),以检索有关该书的信息和已为该书下的订单的信息书。
SELECT orderline.isbn, title, ordernumber, orderdate, customername, numcopies, orderline.bookprice, authorname
FROM author natural join bookauthor natural join book join orderline natural join bookorder
WHERE orderline.isbn = book.isbn
and book.isbn = "1491936169"
这给了我:
ISBN: 1491936169 Title: Kafka: The Definitive Guide: Real-Time Data and Stream Processing at Scale
Author: Neha Narkhede, Todd Palino, Gwen Shapira
Order Number Date Customer Copies Price Total
N201699998 2016-12-24 Mary Hall 2 33.99 67.98
N201799999 2017-01-03 Aran Clauson 1 33.99 33.99
Total:
然而,有些 isbn 尚未订购,因此不在订单行 table 中,但在书本 table 中,它显示了所有书的 isbn。
我想显示那些没有订单的书的信息,例如:
ISBN: 0387848576 Title: The Elements of Statistical Learning
Author: Jerome Friedman, Trevor Hastie, Robert Tibshirani
No orders
本质上我想要 table 在没有订单的情况下显示 isbn 和 null 值的图书信息。我想这将是某种自然的外部连接,但是我的尝试导致空 tables.
已更新查询尝试使用分组依据
删除重复项SELECT ordernumber, orderdate, customername, orderline.isbn, title, orderline.numcopies, stock, shipmentbook.numcopies as shipcopies, authorname
FROM author natural join bookauthor natural join book left join bookorder natural join orderline
ON book.isbn = orderline.isbn
left join mousavs.shipmentbook
ON book.isbn = shipmentbook.isbn
WHERE stock > orderline.numcopies
GROUP BY ordernumber
ORDER BY orderdate, ordernumber, ISBN
错误代码:1055。SELECT 列表的表达式 #4 不在 GROUP BY 子句中并且包含非聚合列 'mousavs.orderline.isbn',它在功能上不依赖于 GROUP BY 子句[=17= 中的列]
Left join 101...你真的......如果你打算在 RDMS 中工作,你真的需要阅读有关联接的内容。
select
b.isbn
,b.title
,b.bookprice
,b.stock
,a.authorname
,o.ordernumber
,o.numcopies
,o.price
from
book b
inner join
BookAuthor ba on
ba.isbn = b.isbn
inner join
Author a on
a.authorid = ba.authorid
left join
orderline o on
o.isbn = b.isbn
left join
bookorder bo on
bo.ordernumber = o.ordernumber
where
b.isbn = 1491936169