In MySQl, how to get the sum of data for an item from multiple table links -


i have 3 tables below.

table 1 name value  details      1     xxa b     2     xxb c     3     xxc  table 2       name     table1 name    data 1                     1234 2                     2345 3          b            4567 4          c            5678 

and

table 3        name    table2data  amount ran1    1234         10 ran2    2345         20 ran3    4567         30 ran4    4567         40 ran5    5678         50 ran6    1234         60 

i need result of query below

total amount each item in table 1                     table 1 row 1 (a)   table 1 row2 (b)    table 1 row 3 ( c)  ----        90                  70                 50            ---- 

how can achieve same. 3 tables need values, need arrange in columns above.

you use conditional sum. this:

select     sum(case when name='a' table3.amount else 0 end) a,     sum(case when name='b' table3.amount else 0 end) b,     sum(case when name='c' table3.amount else 0 end) c     `table1`     join table2         on table1.name=table2.table1name     join table3         on table2.data=table3.table2data 

Comments