我有查询:
SELECT ordernumber, orderdate, customername, orderline.isbn, title, orderline.numcopies, stock, shipmentbook.numcopies as shipcopies, authorname
FROM mousavs.author natural join mousavs.bookauthor natural join mousavs.book left join mousavs.bookorder
natural join mousavs.orderline
ON book.isbn = orderline.isbn
left join mousavs.shipmentbook
ON book.isbn = shipmentbook.isbn
WHERE stock > orderline.numcopies
ORDER BY orderdate, ordernumber, ISBN
这里有多个字段是相同的,除了一本书有多个作者的authorname。我试图使用" GROUP BY ordernumber"每本书只显示一行。 但是我得到一个错误:`"错误代码:1055。SELECT列表的表达式#4不在GROUP BY子句中,并且包含非聚合列&m ;; mousavs.orderline.isbn'它在功能上不依赖于GROUP BY子句中的列;
预期结果:
ordernumber orderdate customername isbn title numcopies stock shipcopies authorname
N201699998 2016-12-24 "Mary Hall" 1491936169 "Kafka: The Definitive Guide: Real-Time Data and Stream Processing at Scale" 2 14 1 "Neha Narkhede"
N201799999 2017-01-03 "Aran Clauson" 1491936169 "Kafka: The Definitive Guide: Real-Time Data and Stream Processing at Scale" 1 14 1 "Gwen Shapira"
N201700004 2017-03-01 "Chris Reedy" 0321399420 "Databases, Types and the Relational Model" 1 5 1 "Hugh Darwen"
N201700003 2017-05-01 "Filip Jagodzinski" 0321399420 "Databases, Types and the Relational Model" 1 5 1 "Hugh Darwen"
N201700006 2017-05-15 "Chris Reedy" 1118063333 "Operating System Concepts" 1 16 NULL "Peter Galvin"
N201700006 2017-05-15 "Chris Reedy" 1449328016 "Database Design and Relational Theory: Normal Forms and All That Jazz (Theory in Practice)" 1 3 NULL "C. J. Date"
更简单的查询:
SELECT book.isbn, title, ordernumber, orderdate, customername, numcopies, orderline.bookprice, authorname
FROM author natural join bookauthor natural join book left join orderline natural join bookorder
ON book.isbn = orderline.isbn
WHERE book.isbn = ?
答案 0 :(得分:0)
查看提供的示例,您可以使用聚合函数(例如:authorname上的min)
SELECT
ordernumber
, orderdate
, customername
, orderline.isbn
, title
, orderline.numcopies
, stock
, shipmentbook.numcopies as shipcopies
, min(authorname)
FROM mousavs.author natural join mousavs.bookauthor natural join mousavs.book left join mousavs.bookorder
natural join mousavs.orderlin ON book.isbn = orderline.isbn
left join mousavs.shipmentbook ON book.isbn = shipmentbook.isbn
WHERE stock > orderline.numcopies
GORUP BY ordernumber
, orderdate
, customername
, orderline.isbn
, title
, orderline.numcopies
, stock
, shipmentbook.numcopies as shipcopies
ORDER BY orderdate, ordernumber, ISBN
但你也可以使用group_concat(authorname)获得单行结果,其中所有的autor都在行中
SELECT
ordernumber
, orderdate
, customername
, orderline.isbn
, title
, orderline.numcopies
, stock
, shipmentbook.numcopies as shipcopies
, group_concat(authorname)
FROM mousavs.author natural join mousavs.bookauthor natural join mousavs.book left join mousavs.bookorder
natural join mousavs.orderlin ON book.isbn = orderline.isbn
left join mousavs.shipmentbook ON book.isbn = shipmentbook.isbn
WHERE stock > orderline.numcopies
GORUP BY ordernumber
, orderdate
, customername
, orderline.isbn
, title
, orderline.numcopies
, stock
, shipmentbook.numcopies as shipcopies
ORDER BY orderdate, ordernumber, ISBN