mysql - SELECT & Calculate SQL but answer wrong -


i want select data mysql. , wanted calculation @ query excution time. had 2 sql tables:

loan table :

loan_id|customer_name|total_amount 1000   |sashika      |55000 1001   |amell        |11000 

settlement table :

sett_id|loan_id|amount a123    |1000    |1000 b123    |1001    |2000 

thats how 2 tables like. want due value of loans. used below code generate wont' show correct values.

select loan.loan_id,loan.customer_name,loan.total_amount,sum(settlement.amount) 'total received',       ((loan.total_amount)-sum(settlement.amount ))as 'total due' loan , settlement loan.loan_id = 1001; 

this code value of total(column) of settelement table. when try loan id(where loan_id=) amount of total of settlement value show , calculate. isn't value want. please me guys

loan_id| customer_name|total_amount|total_received|total_due 1000       |sashika       |55000       |1000          |54000 

this how want edit question , make more understandable :)

you should use inner join , group

select      loan.loan_id    ,loan.customer_name    ,loan.total_amount    ,sum(settlement.amount) 'total received'    ,((loan.total_amount)-sum(settlement.amount ))as 'total due'  loan  inner join settlement  on  loan.loan_id =  settlement.loan_id loan.loan_id=1001 group load.loan_id 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -