如何使用变量从php mysql中的两个表中减去

时间:2015-10-02 12:40:41

标签: php mysql

我从我的第一个表(收入)得到这个总数,我想从另一个表(exp)查询中减去它。所有这些代码都在同一页面上。那么我如何使用这些查询变量在同一页面上减去和回显结果,但在另一个位置呢? 查询1

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL THEN (SELECT lic_num, vehicle_make, vehicle_model, zone.name FROM n' at line 3

3 个答案:

答案 0 :(得分:1)

检查数据类型是否设置为int。

$myVar = $row['SUM(inamount)'] - $row['SUM(amount)'];
echo $myVar;

另一种方法是将两个值分配给它们自己的变量然后执行计算:

$totalSales = $row['SUM(inamount)'];
$expense = $row['SUM(amount)'];

$actual = $totalSales - $expense;

echo $actual;

使用var_dump()将告诉您$ row变量中实际保存的值。这将为您提供有关数据是否正确地从数据库中提取到变量中的线索。

var_dump($row['SUM(inamount)']);
var_dump($row['SUM(amount)']);

要检查的另一件事是,是否设置了实际变量,整体解决方案可能如下所示:

<?php 
include("db.php");

//query 1
$query_income = "SELECT source, SUM(income.inamount) FROM income"; 
$result_income = mysql_query($query_income) or die(mysql_error());
while($row = mysql_fetch_array($result_income)){
$inamount = $row['inamount'];
}

//query 2
$query_exp = "SELECT amount, SUM(exp.amount) FROM exp"; 
$result_exp = mysql_query($query_exp) or die(mysql_error());
while($row = mysql_fetch_array($result_exp)){
$amount = $row['amount'];
}

if(isset($inamount, $amount))  {
 $actual = $amount - $inamount;
 echo $actual // obviously you format $actual to your preferred     output
 }
 else {
   echo "Variable data not set";
 }
?>

答案 1 :(得分:0)

在回显总销售额后,将$ row ['SUM(inamount)']的值分配给变量,您可以使用它来执行减法:

...
$var=$row['SUM(inamount)'];
...
//after printing total expenditure
echo "Total Profit  = ". ($var-$row['SUM(amount)']);
但是,有两项意见: 1. mysql libary已被弃用,请改用mysqli或PDO。 2.如果2个代码在同一页面中,则不需要包括db.php两次。

答案 2 :(得分:0)

我最好的建议应该是只进行一次查询并处理查询中的减法,你可以按照以下方式进行:

<?php 
include("db.php");
$query = "SELECT inc.source, SUM(inc.inamount) as in_amount, SUM(exp.amount) as out_amount, SUM(inc.inamount)-SUM(exp.amount) as total FROM income as inc, exp"; 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "Total Sales  = N". $row['in_amount'];
echo "<br />";
echo "Expenditure Sales  = N". $row['out_amount'];
echo "<br />";
echo "Total after substraction  = N". $row['total'];
echo "<br />";
}
?>

但是如果你真的想用变量进行减法,那么你可以按照以下方式进行减法:

<?php 
include("db.php");
$query = "SELECT source, SUM(inamount) as in_amount FROM income"; 
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
$total_sales = $row['in_amount'];
echo "Total Sales  = N". $row['in_amount'];
echo "<br />";
}
?>

<?php
include("db.php");
$query = "SELECT SUM(amount) as out_amount FROM exp"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
$total_expenditure = $row['out_amount'];
echo "Total Expenditure  = N". $row['out_amount'];
echo "<br />";
}
?>

<?php
$total = $total_sales - $total_expenditure;
echo "Total after substract = N". $total;
echo "<br />";
?>

由您决定使用哪个选项,但我建议您不要进行两个不同的查询来获取两个值,因为它们可能会在一个时间范围内发生变化。