How can I get the value of "profit" to be returned by MySQL. totalCost and totalSell are obtained from the database.
select
sum(cost) as totalCost
, sum(sell) as totalSell
, (totalSell-totalCost) as profit
from orders
Why don't you try to directly count profit instead of using aliases?
Try this:
Select SUM(cost) AS totalCost
, SUM(sell) AS totalSell
, (SUM(sell) - SUM(cost)) AS profit
FROM orders
You can't use aliases in the select portion of the query that is declaring them:
select sum(cost) as totalCost,
sum(sell) as totalSell,
sum(sell) - sum(cost) as profit
from orders
If you wanted to do this using an alias (for some reason), you can do so when defining them in a subquery:
select *, totalCost - totalSell as profit
from ( select sum(cost) as totalCost,
sum(sell) as totalSell,
from orders ) t