如何在一个查询中完成多个查询

时间:2021-07-11 11:06:24

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

4 个解决方案

#1


1  

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

在存储过程中填充它?PHP的数据库驱动程序不会让您运行与之分离的多个查询;出于安全原因。

#2


1  

Have you tried mysqli drivers instead of mysql?

你试过用sqmyli驱动而不是mysql吗?

Take a look at: mysqli_multi_query

查看一下:mysqli_multi_query。

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

执行由分号连接的一个或多个查询。

#3


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

#4


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

您可以使用MySQL UNION -但是您必须对resultset进行迭代,因为在这种情况下,您将获得4条记录,而不是一个/语句组

#1


1  

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

在存储过程中填充它?PHP的数据库驱动程序不会让您运行与之分离的多个查询;出于安全原因。

#2


1  

Have you tried mysqli drivers instead of mysql?

你试过用sqmyli驱动而不是mysql吗?

Take a look at: mysqli_multi_query

查看一下:mysqli_multi_query。

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

执行由分号连接的一个或多个查询。

#3


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

#4


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

您可以使用MySQL UNION -但是您必须对resultset进行迭代,因为在这种情况下,您将获得4条记录,而不是一个/语句组