如何对 Mysql 和两个表进行求和查询

How can I make a Sum Query on Mysql and Two Tables

我需要一个查询的帮助,但我不知道该怎么做。 我有一个 Table (TblParts) 字段:

代码库存 StockReplace

 1    10    0
 2     5    0
 3    15    0
 11    0    0
 22    0    0
 33    0    0

和另一个 Table (TblSust) 带字段

代码替换

 1      11
 11      1
 2      22
 22      2

这个table显示每个代码的替换代码,比如我没有代码1的库存,我可以用代码11的库存,反之也可以。

我需要一个查询来添加每个 TblParts.stockreplace(仅在 TblParts.stock = 0 的地方),TblParts.stock 的总和,其中 TblSust.code = TblParts.code

例如,TblParts 的结果将是:

代码库存 StockReplace

1      10     0
2      5      0
3      15     0
11     0     10
22     0     5
33     0     0

但是如果我在每个之间有两个替换,则必须对每个进行求和,然后计算 TblParts.replacestock 总和。

有人帮忙做这个吗? 谢谢

加入表格并使用相关子查询来获取替换 NB 我已经更改了您的示例数据

drop table if exists tblparts,tblsust;
create table tblparts
(Code int, Stock int, StockReplace int);
insert into tblparts values
( 1  ,   0  ,  0),
( 2  ,   5  ,  0),
( 3  ,  15  ,  0),
( 11 ,  10  ,  0),
( 22 ,   0  ,  0),
( 33 ,   0  ,  0);

create Table TblSust
(Code int, `Replace` int);
insert into tblsust values
( 1   ,   11),
( 11  ,    1),
( 2   ,   22),
( 22  ,    2);

select p.code,p.stock,
        coalesce(
        (select p1.stock from tblparts p1 where p1.code = s.`replace`) 
        ,0) r
from tblparts p
left join tblsust s on s.code = p.code
order by p.code
;

+------+-------+------+
| code | stock | r    |
+------+-------+------+
|    1 |     0 |   10 |
|    2 |     5 |    0 |
|    3 |    15 |    0 |
|   11 |    10 |    0 |
|   22 |     0 |    5 |
|   33 |     0 |    0 |
+------+-------+------+
6 rows in set (0.001 sec)

replace 是保留字,因此在此处反引号。

如果有多个替换,将股票价值相加然后加入

create table tblparts
(Code int, Stock int, StockReplace int);
insert into tblparts values
( 1  ,   0  ,  0),
( 2  ,   5  ,  0),
( 3  ,  15  ,  0),
( 11 ,  10  ,  0),
( 15 ,   2  ,  0),
( 22 ,   0  ,  0),
( 33 ,   0  ,  0);

create Table TblSust
(Code int, `Replace` int);
insert into tblsust values
( 1   ,   11),
( 11  ,    1),
( 2   ,   22),
( 22  ,    2),
(  1  ,   15);

select p.code,p.stock,
        coalesce(
        s.stockreplace
        ,0) r
from tblparts p
left join 
(select s.code, sum(stock) stockreplace
from tblsust s
join tblparts p  on s.`replace`= p.code
group by s.code
order by p.code
) s on p.code = s.code
;

+------+-------+------+
| code | stock | r    |

+------+-------+------+
|    1 |     0 |   12 |
|    2 |     5 |    0 |
|    3 |    15 |    0 |
|   11 |    10 |    0 |
|   15 |     2 |    0 |
|   22 |     0 |    5 |
|   33 |     0 |    0 |
+------+-------+------+
7 rows in set (0.001 sec)

加入tblparts到计算总和的查询:

update tblparts p
inner join (
  select s.code, sum(p.stock) total
  from tblsust s 
  inner join tblparts p on s.replace = p.code
  group by s.code
) t on t.code = p.code  
set p.stockreplace = t.total
where p.stockreplace = 0;

参见demo
结果:

| Code | Stock | StockReplace |
| ---- | ----- | ------------ |
| 1    | 10    | 0            |
| 2    | 5     | 0            |
| 3    | 15    | 0            |
| 11   | 0     | 10           |
| 22   | 0     | 5            |
| 33   | 0     | 0            |