MySQL查询总ID百分比

时间:2016-02-12 18:15:33

标签: mysql

我有一张这样的表:

"Order1", "Product1", "3"
"Order1", "product2", "5"
"Order2", "product1", "2"

等等,我想要一个输出如下输出的查询:

"Order1", "Product1", "3/8"
"Order1", "product2", "5/8"
"Order2", "product1", "2/n"

8是每个订单上产品总量的总和。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

这对你有用:

SELECT s.ordernr
      ,p.product
      ,CONCAT(p.quantity, '/', sum(s.quantity) AS percentage
FROM yourtable s
LEFT JOIN yourtable p ON p.orderid = s.oerderid
GROUP BY s.ordernr
ORDER BY s.ordernr;