3

How can I make the following queries done in one single query and get the result just the way it is in below?

// 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
1
  • 1
    You could use UNION, each row will represent different query. Commented Dec 28, 2011 at 9:36

4 Answers 4

1

Stuff it in a Stored Procedure? The database drivers for PHP won't let you run several queries separated with ; for security reasons.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried mysqli drivers instead of mysql?

Take a look at: mysqli_multi_query

Executes one or multiple queries which are concatenated by a semicolon.

Comments

1
SELECT 'withdrawals' t, SUM( amount ) sum
FROM withdrawals
UNION
SELECT 'statement' t, SUM( amount ) sum
FROM statement

enter image description here

while($row = mysql_fetch_assoc($result))
{
    $total[$row['t']] = $row['sum'];
}

echo $total['withdrawals']; # 100
echo $total['statement']; # 624.x

Comments

0

You could use MySQL UNION - but you'll have to iterate over the resultset, because in that case you will get 4 records instead of one/statement group

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.