我有一个疑问:
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
这里的多个字段是相同的,除了作者姓名(一本书有多个作者)。我尝试使用“GROUP BY ordernumber”为每本书仅显示一行。 但是我收到错误:“错误代码:1055。SELECT 列表的表达式#4 不在 GROUP BY 子句中,并且包含非聚合列“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 = ?
最佳答案
查看提供的示例,您可以使用聚合函数(例如:作者名上的 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) 来获得所有作者都在 ine 行中的单个结果
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
关于mysql - 使用group by从mysql选择查询中删除相似的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44465209/