简体   繁体   中英

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. 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

<?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.

$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. 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:

...
$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. There is no need to include db.php twice, if the 2 pieces of code are in the same page.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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