mysql - how to get sum with table and minus with other table? -


i have 2 tables:

budget

id | project_name | total_budget 1  |    test      | 5000 2  |    try       | 200 

expense

id|  project_name |paper|ballpen|total 1 | test          |100  |200    | 300 2 | test          |50   |50     |100 3 | try           |20   |0      |20 

so total of test project_name 400

5000 - 400 = 4600  

try 200 -20 = 180 want 1 result

sorry im newbie cant one

this simple join aggregation:

select b.project_name, b.total_budget, e.total total_expense, b.total_budget - e.total diff budget b left join      (select project_name, sum(total) total       expense e       group project_name      ) e      on b.project_name = e.project_name; 

if want 1 project_name, add where clause:

where b.project_name = 'test' 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -