如何在一个查询中完成以下查询,并按照下面的方式获得结果?
// Begining of January
$ob = mysql_query(" SELECT SUM(salary_amount) AS total FROM teacherexpense WHERE month(disburse_date)='01' AND year(disburse_date)='$year' ");
$nt = mysql_fetch_assoc($ob);
$salaryamount= $nt['total'];
$ob = mysql_query(" SELECT SUM(other_expense_amount) AS expenseamount FROM otherexpense WHERE month(other_expense_date)='01' AND year(other_expense_date)='$year' ");
$nt = mysql_fetch_assoc($ob);
$expenseamount= $nt['expenseamount'];
$jk = mysql_query(" SELECT SUM(amountpaid) AS revenue FROM studentpayment1 WHERE month(received_date)='01' AND year(received_date)='$year' ");
$t = mysql_fetch_assoc($jk);
$revenue= $t['revenue'];
$ob = mysql_query(" SELECT SUM(other_earning_amount) AS otherearningamount FROM otherearning WHERE month(other_earning_date)='01' AND year(other_earning_date)='$year' ");
$nt = mysql_fetch_assoc($ob);
$otherearningamount= $nt['otherearningamount'];
$January= ($revenue+$otherearningamount)-($salaryamount+$expenseamount);
// End of January
答案 0 :(得分:1)
在存储过程中填充它? PHP的数据库驱动程序不允许您运行多个以;分隔的查询;出于安全考虑。
答案 1 :(得分:1)
答案 2 :(得分:1)
SELECT 'withdrawals' t, SUM( amount ) sum
FROM withdrawals
UNION
SELECT 'statement' t, SUM( amount ) sum
FROM statement
while($row = mysql_fetch_assoc($result))
{
$total[$row['t']] = $row['sum'];
}
echo $total['withdrawals']; # 100
echo $total['statement']; # 624.x
答案 3 :(得分:0)
您可以使用MySQL UNION - 但您必须迭代结果集,因为在这种情况下您将获得4条记录而不是一条/语句组