SELECT *,
SUM(price+shipping+paypalfee+storefee) AS totalcost,
customerpaid AS totalrevenue,
(totalcost - totalrevenue) AS profit
FROM tblsales
GROUP BY orderno
HAVING " . $having . "
ORDER BY $sort $order
LIMIT $offset,$rows
如果我省略(totalcost - totalrevenue) as profit
查询工作正常。如何使用总成本和总收入在同一查询中计算PROFIT?如何从MySQL中的同一个表中减去两个计算的字段?
感谢您的解决方案。请问为什么我应该明确地使用列名?我的意思是背后的逻辑。 – 2014-11-22 14:02:11
@BubbaYakoza。 。 。你明白'group by'是什么吗?在'select'列中有不在'group by'列表中的列通常没有意义。也许关于扩展的文档将有助于:http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html。 – 2014-11-22 14:04:21