简体   繁体   English

如何在php mysql中减去两个表

[英]how to subtract two table in php mysql

i have two table in mysql 1st is fees and 2nd is expenses 我在mysql中有两个表是第一个是费用,第二个是费用

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 这显示了这个23445只有第一个表结果

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. 不要使用mysql_ *系列函数,因为它们将被弃用。 Start to look into mysqli or pdo 开始研究mysqli或pdo

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

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