我有两个表tblsales和tblproduct。 tblsales有| id | pcode |数量|和tblproduct有| id | pcode | pleft | 。我的问题是,我希望从tblsales中总计所有数值,并将其添加到tblproduct中。我尝试了我在MYSQL编程中学到的东西,但它对我不起作用。
例如
tblsales
| id | pcode | qty |
| 1 | 455 | 1 |
| 2 | 455 | 1 |
tblproduct
| id | pcode | pleft |
| 4 | 455 | 5 | <--- this is the current value with out update
这是我的问题。所以如果我点击按钮,必须更新tblproduct。类似这样的结果
tblproduct
| id | pcode | pleft |
| 4 | 455 | 7 | <--- this must be the value after clicking the button.
答案 0 :(得分:0)
这将做的事情:
SELECT (SELECT SUM(qty) FROM tblsales)+(SELECT SUM(pleft) FROM tblproduct) AS total_sum
如果您正在尝试获取所有表格数据。
但是,如果您要按pcode
对其进行分组,则这是不同的任务:
SELECT pcode, SUM(pcode_sum) AS total_sum
FROM
(SELECT pcode, SUM(qty) AS pcode_sum AS FROM tblsales GROUP BY pcode
UNION ALL
SELECT pcode, SUM(pleft) AS pcode_sum FROM tblproduct GROUP BY pcode)
答案 1 :(得分:0)
如果您需要更新表格:
UPDATE tblproduct
SET pleft=pleft+(SELECT SUM(qty)
FROM tblsales
WHERE tblsales.pcode=tblproduct.pcode)
如果您需要,请选择:
SELECT Id,PCode,pleft+(SELECT SUM(qty)
FROM tblsales
WHERE tblsales.pcode=tblproduct.pcode)
FROM tblproduct