mySQL - 列的总和

时间:2017-03-09 09:41:17

标签: mysql

我有2张桌子

表1

id product
1  P1
2  P2

表2

id amount product_t1
1  100    P1
2  200    P1
3  300    P2
4  400    P1
5  500    P2

我希望我的输出为:

product totalAmount(sum of amount)
P1      700
P2      800

编辑:到目前为止,这是我的查询

SELECT T1.product, SUM(T2.amount)
FROM table1 T1
INNER JOIN table2 T2
ON T1.product = T2.product_t1

谢谢!

4 个答案:

答案 0 :(得分:1)

由于您没有使用外键,因此您甚至不需要表1 来获得所需的结果。

np.load(open('file.npy'))

您缺少的是SELECT product_t1 AS product, SUM(amount) AS totalAmount FROM table2 GROUP BY product_t1 声明,以便为每个产品单独添加一行

答案 1 :(得分:1)

你应该使用group by

 SELECT T1.product, SUM(T2.amount)
 FROM table1 T1
 INNER JOIN table2 T2
 ON T1.product = T2.product_t1
 GROUP BY product

答案 2 :(得分:1)

 SELECT T1.product, SUM(T2.amount)
 FROM table1 T1
 INNER JOIN table2 T2
 ON T1.product = T2.product_t1
 GROUP BY product T1.product

使用GROUP BY标记。 Group by有助于根据产品价值对结果进行分组

答案 3 :(得分:0)

尝试使用function display_graph(id, label, data) { var ctx = document.getElementById(id); var data = { labels: data.labels, datasets: [ { label: label, fill: false, lineTension: 0.1, backgroundColor: "rgba(75,192,192,0.4)", borderColor: "rgba(75,192,192,1)", borderCapStyle: 'butt', borderDash: [], borderDashOffset: 0.0, borderWidth: 1, borderJoinStyle: 'miter', pointBorderColor: "rgba(75,192,192,1)", pointBackgroundColor: "#fff", pointBorderWidth: 1, pointHoverRadius: 5, pointHoverBackgroundColor: "rgba(75,192,192,1)", pointHoverBorderColor: "rgba(220,220,220,1)", pointHoverBorderWidth: 2, pointRadius: 1, pointHitRadius: 10, data: data.assay_value, spanGaps: false } ] }; //options var options = { responsive: true, title: { display: true, position: "top", text: label, fontSize: 18, fontColor: "#111" }, legend: { display: true, position: "bottom", labels: { fontColor: "#333", fontSize: 16 } } }; var Blanks_Chart=null; Blanks_Chart = new Chart(ctx, { type: 'line', data: data, options: options });} GROUP BY

AS
  

https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

或者

添加到您的代码组
 SELECT product_t1 as product, SUM(amount) AS totalAmount FROM table2
 GROUP BY product