我有两个表 tblsales 和 tblproduct。 tblsales 有|编号 |密码 |数量|和 tblproduct 有 |编号 |密码 |左 | .我的问题是我想从 tblsales 中计算出所有数量值并将其添加到 tblproduct 中的 pleft。我尝试了我在 MYSQL 编程中学到的知识,但它对我不起作用。
例如
销售
| id | pcode | qty |
| 1 | 455 | 1 |
| 2 | 455 | 1 |
产品
| id | pcode | pleft |
| 4 | 455 | 5 | <--- this is the current value with out update
这是我的问题。所以如果我点击按钮,tblproduct 必须更新。类似这样的结果
产品
| id | pcode | pleft |
| 4 | 455 | 7 | <--- this must be the value after clicking the button.
最佳答案
这会做的事情:
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)
关于php - 将表 1 中的总数量加到表 2 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18248770/