user3188987
user3188987

Reputation: 1

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';

Upvotes: 0

Views: 597

Answers (2)

bilal saeed
bilal saeed

Reputation: 1

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.

Upvotes: 0

Marian
Marian

Reputation: 1164

Try this:

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

Upvotes: 1

Related Questions