drqxfmfa804578 2013-04-27 00: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 01: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部