Reputation: 75
i have two table in mysql 1st is fees and 2nd is expenses
i want fees table subtract in to expenses table
how can i do this please help me to fix this issue thanks
fees table
------------------------------
id | name | grn | fees|
------------------------------
expence table
------------------------------
id | invoice | amount
------------------------------
this table showing only sum of 1st fees table
// connect to the database
include('connect-db.php');
$sql = "select sum(fees) from fees";
$q = mysql_query($sql);
$row = mysql_fetch_array($q);
echo 'Sum: ' . $row[0];
This is showing like this 23445 only 1st table result
and i want like this
23445 fees table result
- 3234 expense table result
------
20211 total income
------
Upvotes: 0
Views: 1669
Reputation: 15981
Here you go:
// connect to the database
include('connect-db.php');
$sql = "select sum(fees) from fees";
$q = mysql_query($sql);
$fees = mysql_fetch_array($q);
$sql = "select sum(amount) from expence";
$q = mysql_query($sql);
$expense = mysql_fetch_array($q);
echo 'Fees: ' . $fees[0].'<br>';
echo 'Expenses: ' . $expense[0].'<br>';
echo 'Income: ' . ($fees[0] - $expense[0]) .'<br>';
Note:
Don't use mysql_* family functions because they are going to deprecated. Start to look into mysqli or pdo
Upvotes: 1
Reputation: 157334
So store the results in a variable and subtract it, SIMPLEST WAY YOU CAN DO IT
$fees_amt = mysql_query($sql); //Returns say 10
$exp_amt = mysql_query($sql2); //Returns 5
$tot_amt = $fees_amt - $exp_amt;
echo $tot_amt;
Upvotes: 1