简体   繁体   中英

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).

Here's the challenge. The columns XXS, XS, SM, MD, LG contain numbers. 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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