MYSQL - update each row from a table to set a column with other specific value on other table -


i have 2 tables this:

table a
ces | reg | year | inc | ouc
---------------------------------------
c1   | usa   | 2015   | 0     | 0
c2   | uk     | 2014   | 0     | 0
c3   | br     | 2015   | 0     | 0
c1   | ru     | 2016   | 0     | 0
c1   | usa   | 2016   | 0     | 0

table b
ces | reg | year | val   | dis(%)
-----------------------------------------
c1   | usa   | 2015  | 100    | 10
c1   | usa   | 2015  | 200    | 20
c1   | ru     | 2016  | 200    | 10
c1   | usa   | 2016  | 500    | 20
c2   | uk     | 2014  | 200    | 20
c2   | uk     | 2014  | 500    | 10
c3   | br     | 2015  | 1000  | 30
c3   | br     | 2015  | 500    | 10
c3   | br     | 2015  | 200    | 20

i want update table table b year = 2015 :
ces | reg | year | inc  | ouc
---------------------------------------
c1   | usa   | 2015   | 250   | 0
c2   | uk     | 2014   | 0       | 0
c3   | br     | 2015   | 1310 | 0
c1   | ru     | 2016   | 0       | 0
c1   | usa   | 2016   | 0       | 0

*) note :
table_a.inc = sum(table_b.val * table_b.dis / 100) table update table b ces, reg , year same

for c1, usa, 2015 :
row 1 + row 2 table b = (100*(100-10)/100) + (200*(100-20)/100)

for c3, br, 2015 :
row 7 + row 8 + row 9 table b = (1000*(100-30)/100) + (500*(100-10)/100) + (200*(100-20)/100)

could single query?
thx

========================================================
oh well... ive got answer yesterday...

update set inc = (select sum(val*(100-dis)/100) b a.ces=b.ces , a.reg=b.reg , a.year=b.year) a.year = '2015' 

thx :d

try below:

update table_a a, table_b b set inc = (b.`val` * b.`dis` / 100) a.`ces` = b.`ces`     , a.`reg`  = b.`reg`      , a.`year` = b.`year`     ; 

sql fiddle : http://sqlfiddle.com/#!9/55519


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -