drqxfmfa804578 2013-04-27 08:04
浏览 30
已采纳

如何在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 
  ------
  • 写回答

2条回答 默认 最新

  • doufu9145 2013-04-27 09:10
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下