简体   繁体   English

MySQL SQL查询格式化报告

[英]Mysql sql query to format a report

This is my table product_details : 这是我的表product_details

Product_Code | Size | Quantity
-------------+------+-----------
CS01         | 10   | 15
CS01         | 11   | 25
CS01         | 12   | 35
PR01         | 40   | 50
PR01         | 41   | 60

I want a the following format for a report to get the total quantity group by product code (all sizes of product code): 我希望报表采用以下格式,以便按产品代码(所有尺寸的产品代码)获取总数量组:

Product_Code | Size       | Quantity
-------------+------------+----------------
CS01         | 10  11  12 | 75
PR01         | 40  41     | 110 

I tried the following query but it does not give the result I want. 我尝试了以下查询,但没有给出我想要的结果。

SELECT product_no, size, SUM(quantity) 
FROM product_details 
GROUP BY product_no;

Please help me to find the query to format the report. 请帮助我找到查询以格式化报告。

You can use group concat 您可以使用group concat

SELECT 
product_no, 
group_concat(size  SEPARATOR ' '),
sum(quantity) 
FROM product_details group by product_no;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM