从另一列中减去一列的总数 - PHP / MySQL

时间:2014-07-22 15:17:55

标签: php mysql sql

我尝试做的是从另一整列的总和中减去整个MySQL列的总和。我可以分别成功地对两个列进行求和,但我无法弥补它们之间的差距,然后从另一列中减去一列。

$start = "SELECT SUM(start) FROM `table`";
$end = "SELECT SUM(end) FROM `table`";
$total = "Here is my problem"; //I tried something like $end - $start, but to no avail

我可以显示开始和结束之和,但不能结束 - 开始。

3 个答案:

答案 0 :(得分:0)

你试过了吗?

SELECT (SUM(start) - SUM(end)) AS `difference` FROM `table`

答案 1 :(得分:0)

我想即使这样也行不通:

SELECT SUM(`start` -`end`) AS `total` FROM `table`

答案 2 :(得分:0)

如其他人所述,您可以选择已计算的总和

$totalsql = "SELECT (SUM(start) - SUM(end)) AS difference FROM table";
//Query to call would be here $result = $db->query($totalsql) or whatever
$total = $result['difference'];