mysql - 使用group by从mysql选择查询中删除相似的字段

标签 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 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

我用它得到了表格: enter image description here

这里的多个字段是相同的,除了作者姓名(一本书有多个作者)。我尝试使用“GROUP BY ordernumber”为每本书仅显示一行。 但是我收到错误:“错误代码:1055。SELECT 列表的表达式#4 不在 GROUP BY 子句中,并且包含非聚合列“mousavs.orderline.isbn”,该列在功能上不依赖于 GROUP BY 子句中的列;

我的数据库结构:enter image description here

预期结果:

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/

相关文章:

c++ - Wt Dbo MySQL 后端连接错误 'SET storage_engine=INNODB;' : Unknown system variable 'storage_engine'

php - 数据库 SQL 按列字段中的值排序

考虑缺失值的 Pandas 滚动聚合

Python按两个列值的相反排序顺序按Groupby连接两个数据框

php - 数据库图库数组

MySQL 多级父子 SP 和 IN 子句

sql - sql server 2008中十进制到十六进制的转换

mysql 过程 order by 子句条件

pythonic方式对数据框中的重复行进行排名然后合并

mysql - 查询中的选择命令