php - Update sum from another table -


i have items movements , inventory table , report table.

movements table:

 ------------------------------------ id   | part_number | amount | code | ------------------------------------ 101  | 3-80-75     |  55    |  2 102  | 5-88-70m    |  -65   |  1 103  | 1-46-57     |  11    |  1 104  | 3-80-75     |  -8    |  2    105  | 5-51-54     |  76    |  2 106  | 3-80-75     |  -22   |  1 107  | 5-51-54     |  62    |  1 108  | 1-46-57     |  -3    |  2 109  | 3-80-75     |  -16   |  2 ----------------------------------- 

report table:

 ---------------------------------- id  | part_number  | total_code2  | ---------------------------------- 456 |  3-80-75     |    457 |  5-88-70m    |    458 |  1-46-57     |    459 |  3-80-75     |    460 |  5-51-54     |    ---------------------------------- 

i need update report table sum of code 2 only, each part number.

the result need this:

 ---------------------------------- id  | part_number  | total_code2  | ---------------------------------- 456 |  3-80-75     |   31 457 |  5-88-70m    |   0 458 |  1-46-57     |   -3 459 |  3-80-75     |   -24 460 |  5-51-54     |   76 ---------------------------------- 

report table shown in question had 2 rows @ least 1 part

so added code column report table.

only 3 rows updated based on code 2.

matches expected results after allowing question snafu, imho

ddl:

create table movements (   id int not null,     part_number varchar(40) not null,     amount int not null,     code int not null ); -- truncate table movements; insert movements (id,part_number,amount,code) values (101,'3-80-75',55,2); insert movements (id,part_number,amount,code) values (102,'5-88-70m',-65,1); insert movements (id,part_number,amount,code) values (103,'1-46-57',11,1); insert movements (id,part_number,amount,code) values (104,'3-80-75',-8,2);    insert movements (id,part_number,amount,code) values (105,'5-51-54',76,2); insert movements (id,part_number,amount,code) values (106,'3-80-75',-22,1); insert movements (id,part_number,amount,code) values (107,'5-51-54',62,1); insert movements (id,part_number,amount,code) values (108,'1-46-57',-3,2); insert movements (id,part_number,amount,code) values (109,'3-80-75',-16,2);  create table report (   id int not null,     part_number varchar(40) not null,     code int not null,     total_code2 int not null ); -- truncate table report; insert report (id,part_number,code,total_code2) values (456,'3-80-75',2,0); insert report (id,part_number,code,total_code2) values (458,'1-46-57',2,0); insert report (id,part_number,code,total_code2) values (460,'5-51-54',2,0); 

update:

update report r join     (   select part_number, sum(amount) total_code         movements         code = 2          group part_number     ) m on m.part_number = r.part_number set total_code2 = m.total_code r.code=2  --   <----- update code 2 

results:

select * report;  +-----+-------------+------+-------------+ | id  | part_number | code | total_code2 | +-----+-------------+------+-------------+ | 456 | 3-80-75     |    2 |          31 | | 458 | 1-46-57     |    2 |          -3 | | 460 | 5-51-54     |    2 |          76 | +-----+-------------+------+-------------+ 3 rows in set (0.00 sec) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -