简体   繁体   English

总和多列,但在MySQL查询一

[英]Sum multiple columns but one in mysql query

Ok, so, I have a product table. 好的,所以我有一个产品表。 Table name is 'orders'. 表名是“订单”。 Here are the relevant columns: 以下是相关的列:

orders table: ID, Style, Color, XXS, XS, SM, MD, LG, Other

I will be providing a comma separated list of numbers that correspond to values in the ID column (it's the primary key). 我将提供一个逗号分隔的数字列表,这些列表与ID列中的值相对应(这是主键)。

Here's the challenge. 这是挑战。 The columns XXS, XS, SM, MD, LG contain numbers. XXS,XS,SM,MD,LG列包含数字。 the Column Other contains text. 其他列包含文本。 Whenever we enter an order, they will enter numbers into those columns, but then a text description of something else into the last column. 每当我们输入订单时,他们都会在这些栏中输入数字,然后在最后一栏中输入其他内容的文字说明。 Here's a few example rows: 这是一些示例行:

Style: 2000, Color: RED. XS: 5, MD: 5, Other: Youth 5-L
Style: 2000, Color: RED, XS: 3, L: 15
Style: 2000, Color: RED, Other: Youth 15 XS
Style: 2000, Color: RED, MD: 10, L:10
Style: 2000, Color: BLACK, MD: 15, Other: Youth - 10-L
Style: 2000, Color: BLACK, MD: 20, LG: 25

What I need is a query that groups them by Style, Then Color, and sums up each of the columns XXS, XS, SM, MD, and LG (individually), but spits out the text from the other column. 我需要的是一个按样式,然后按颜色分组的查询,并分别汇总了XXS,XS,SM,MD和LG列中的每一个,但从另一列中吐出了文本。 I also need this list sorted by Style, and then Color. 我还需要按样式,然后按颜色排序的此列表。 Lastly, it needs to be a list of rows that are present in a comma separated list of values. 最后,它必须是用逗号分隔的值列表中存在的行列表。 Here's the desired output from the table above: 这是上表中的期望输出:

Style: 2000, Color: BLACK. MD: 35, LG: 25
Style: 2000, Color: BLACK. Other: Youth - 10-L
Style: 2000, Color: RED. XS: 8, MD: 15, L: 25
Style: 2000, Color: RED. Other: Youth 5-L
Style: 2000, Color: RED. Other: Youth 15 XS

I hope that makes sense! 我希望这是有道理的! We need to keep the rows separate because they are entered by Customer, and we need to preserve exactly who ordered what. 我们需要将行分开,因为它们是由客户输入的,并且我们需要准确地保留谁订购了什么。

BONUS POINTS if the Other column can be concatenated into a string separated by a special unique sequence of chars if it's the same style and color (use |-| as the separator) Example below: 如果“其他”列可以连接到一个由特殊的唯一字符序列分隔的字符串中,则奖励点,如果它具有相同的样式和颜色(使用|-|作为分隔符),则示例如下:

Style: 2000, Color: BLACK. MD: 35, LG: 25, Other: Youth - 10-L<br>
Style: 2000, Color: RED. XS: 8, MD: 15, L: 25, Youth 5-L|-|Youth 15-XS

EDIT!!!!! 编辑!!!!!

I think I spoke to soon, and solved it myself. 我想我很快就和我解决了。 If anyone is curious, here is the query in question. 如果有人好奇,这里是有问题的查询。 I can't answer this myself for more than a few hours. 我自己不能回答超过几个小时的时间。

SELECT SUM(XXS) as XXS, SUM(XS) as XS, SUM(SM) as SM, SUM(MD) as MD, SUM(LG) as LG GROUP_CONCAT(Other SEPARATOR '|-|') as Other, Style, Color
FROM orders
WHERE ID IN(List...)
GROUP BY Style, Color
ORDER BY Style, Color;

This query groups together all rows based on similar values in Style and Color, sums the columns with numbers in them, and Concatenates together all the columns with text into a format parse-able by my program. 该查询根据“样式”和“颜色”中的相似值将所有行分组在一起,对其中具有数字的列进行求和,然后将所有具有文本的列合并为我的程序可解析的格式。

Just to allow you to accept the answer, here it is: 只是为了让您接受答案,这里是:

SELECT SUM(XXS) as XXS, SUM(XS) as XS, SUM(SM) as SM, SUM(MD) as MD, SUM(LG) as LG GROUP_CONCAT(Other SEPARATOR '|-|') as Other, Style, Color
FROM orders
WHERE ID IN(List...)
GROUP BY Style, Color
ORDER BY Style, Color;
SELECT
    SUM(XXS) as XXS,
    SUM(XS) as XS,
    SUM(SM) as SM,
    SUM(MD) as MD,
    SUM(LG) as LG
GROUP_CONCAT(Other SEPARATOR '|-|') as Other, Style, Color
FROM orders
WHERE ID IN(List...)
GROUP BY Style, Color
ORDER BY Style, Color;

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

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