简体   繁体   English

MYSQL SUM或两列匹配时合并一列

[英]MYSQL SUM or Combine one column when two columns match

I tried and tried but I am just not getting it. 我试了又试,但我还是不明白。

What I am try to do is this. 我试图做的就是这个。 I have one table and when there are matching productCode's with matching productPrice's, I need the results to combine the matching productCodes, sum the productUnits of the matching productCode's then multiply them by the productPrice. 我有一张表,当有匹配的productCode与匹配的productPrice时,我需要结果来组合匹配的productCode,将匹配的productCode的productUnit相加,然后将它们乘以productPrice。

  • note : these have matching prices and productcode. 注意:这些具有匹配的价格和产品代码。

Example

I attached a picture of my example because I suck at HTML formatting also. 我附上示例图片,因为我也很喜欢HTML格式。

You can use GROUP BY with multiple columns, to combine all the rows where those columns match. 您可以将GROUP BY与多列一起使用,以合并那些列匹配的所有行。 Then just sum the productUnits in those rows. 然后只需将这些行中的productUnits相加productUnits

SELECT productCode, productPrice, SUM(productUnits) AS productUnits, productPrice * SUM(productUnits) AS totals
FROM orders_products
GROUP BY productCode, productPrice

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

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