简体   繁体   中英

how to subtract two table in php mysql

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

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;

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

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