Select multiple columns from a table, but group by one

后端 未结 11 1439
逝去的感伤
逝去的感伤 2020-12-12 14:54

The table name is \"OrderDetails\" and columns are given below:

OrderDetailID || ProductID || ProductName || OrderQuantity

I\'m trying to s

11条回答
  •  半阙折子戏
    2020-12-12 15:40

    mysql GROUP_CONCAT function could help https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat

    SELECT ProductID, GROUP_CONCAT(DISTINCT ProductName) as Names, SUM(OrderQuantity)
    FROM OrderDetails GROUP BY ProductID
    

    This would return:

    ProductID     Names          OrderQuantity
    1001          red            5
    1002          red,black      6
    1003          orange         8
    1004          black,orange   15
    

    Similar idea as the one @Urs Marian here posted https://stackoverflow.com/a/38779277/906265

提交回复
热议问题