简体   繁体   English

如何使用变量从PHP MySQL中的两个表中减去

[英]How to subtract from two tables in php mysql using variables

i have this total from my first table (income) and I would want to subtract it from the another table(exp) query. 我从我的第一个表(收入)中得到了总计,我想从另一个表(exp)查询中减去它。 All these code are on thesame page. 所有这些代码都在同一页面上。 So how can i use these query variables to subtract and echo result on thesame page but in another location? 那么,如何使用这些查询变量在同一页面上但在另一个位置中减去并回显结果呢? Query 1 查询1

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

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


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

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

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

Check that the data type is set to int. 检查数据类型是否设置为int。

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

Another approach is to assign the two values to their own variable then perform the calculation: 另一种方法是将两个值分配给它们自己的变量,然后执行计算:

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

$actual = $totalSales - $expense;

echo $actual;

Using var_dump() will tell you what values are actually being held in the $row Variables. 使用var_dump()会告诉您$ row变量中实际保留了哪些值。 This will give you clues as to whether the data is getting pulled from the database into the variables properly. 这将为您提供有关是否将数据正确地从数据库拉入变量的线索。

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

Another thing to check is if the actual variables are being set so overall the solution might look like this: 要检查的另一件事是是否设置了实际变量,因此总体而言,解决方案可能如下所示:

<?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";
 }
?>

Assign the value of $row['SUM(inamount)'] to a variable after echoing total sales, which you can use to perform the subtraction: 回显总销售额后,将$ row ['SUM(inamount)']的值分配给变量,您可以使用该变量执行减法:

...
$var=$row['SUM(inamount)'];
...
//after printing total expenditure
echo "Total Profit  = ". ($var-$row['SUM(amount)']);

2 observations, though: 1. mysql libary is already deprecated, use mysqli or PDO instead. 不过有2个观察结果:1. mysql libary已被弃用,请改用mysqli或PDO。 2. There is no need to include db.php twice, if the 2 pieces of code are in the same page. 2.如果这两个代码在同一页面中,则无需两次包含db.php。

My best suggestion should be to make just one query and process the substraction within the query, you can make it as follow: 我最好的建议应该是只进行一个查询并处理查询中的减法,您可以按照以下步骤进行:

<?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 />";
}
?>

But if you strictly want to make the substraction with the variables, then you can make it as follow: 但是,如果您严格希望使用变量进行减法,则可以按照以下步骤进行:

<?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 />";
?>

Is up to you what option you would like to use, but i suggest not to make two different queries to get two values, because they could change within a timeframe. 由您决定要使用什么选项,但是我建议不要进行两个不同的查询来获取两个值,因为它们可能会在一个时间范围内更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM