使用GROUPBY从mysql select查询中删除类似字段
我有一个疑问:使用GROUPBY从mysql select查询中删除类似字段,mysql,sql,group-by,Mysql,Sql,Group By,我有一个疑问: 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
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子句中,并且包含未聚合列“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 = ?
查看提供的示例,您可以在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
查看您的示例,您没有重复的行,因此如果您想删除列中的相同值,您应该在presentation..端客户端而不是mysql中工作。。否则,您可以使用聚合函数从multipleSee中仅获取一个值,以便group by函数工作,您还需要添加isbn列,因为我假设它是一个外键。然而,这将有损于该小组的目的。我相信解决这个问题的唯一方法是使用子查询。向您提问,并根据示例添加实际的预期结果provided@ChrisThornton确切地说,它需要我的isbn、ShipCopy和authorname,但不知道子查询应该如何形成。