[英]how to subtract two table in php mysql
我在mysql中有两个表是第一个是费用,第二个是费用
我希望费用表减去费用表
我怎么能这样做,请帮我解决这个问题谢谢
费用表
------------------------------
id | name | grn | fees|
------------------------------
expence table
------------------------------
id | invoice | amount
------------------------------
此表仅显示第一笔费用表的总和
// 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];
这显示了这个23445只有第一个表结果
and i want like this
23445 fees table result
- 3234 expense table result
------
20211 total income
------
因此,将结果存储在变量中并减去它,简单地说,你可以做到这一点
$fees_amt = mysql_query($sql); //Returns say 10
$exp_amt = mysql_query($sql2); //Returns 5
$tot_amt = $fees_amt - $exp_amt;
echo $tot_amt;
干得好:
// 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>';
注意:
不要使用mysql_ *系列函数,因为它们将被弃用。 开始研究mysqli或pdo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.