简体   繁体   中英

How to minus / subtract the values after getting their sum in mysql

I want to get value from sum of dueamount, sum of received amount as well remaining amount . first two answers are being retrieved but remaining amount(which will come from the subtraction of those two values) is not coming from the query. Please help me.

$query= "SELECT SUM(dueamount) AS dueamount,SUM(receivedamount) AS received, 'SUM(dueamount)'-'SUM(receivedamount)' AS due from paymentdetails where regno='$regno';

Try this:

$query= "SELECT SUM(dueamount) AS dueamount, SUM(receivedamount) AS received, 
dueamount - received AS due from paymentdetails where regno='$regno';

Its the single quote thats causing the issue, single quote should be around column name not the whole aggregate function because it consider it a string liternal then so no subraction is performed on two numbers but literals which result in 0.

$query= "SELECT SUM(dueamount) AS dueamount,SUM(receivedamount) AS received, SUM('dueamount')-SUM('receivedamount') AS due from paymentdetails where regno='$regno'

Hope it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM