MySQL Forums
Forum List  »  Other Migration

Subtract 2 different tables
Posted by: Larry Dobbs
Date: May 09, 2007 09:35PM

I have included how I can sub from 2different tables. Now I want to subtract TOTAL PAYMENTS from TOTAL PRINCIPAL.

Total Principal
<?php
$query = "SELECT sum(totalprincipal) AS totalprincipal FROM rentals,inventory where rentals_cst_id='$id' AND subid='$subid' AND rentals_inv_id = num AND rentals_active = 'Yes'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "$". $row['totalprincipal']; }?>

------------------------------

Total Payments:
<?php
$query = "SELECT sum(principal) AS totalpayments FROM payments where payments_id='$id' AND payments_subid='$subid' AND del='No'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "$". $row['totalpayments']; }?>

------------------------------------------

I found a similar code but cannot seem to get it to work, here is what I have started with errors:

Balance Due:
<?php
$query = "SELECT tmp.subid, sum(tmp.totaldue) AS balancedue FROM [ SELECT rentals.subid, sum(totalprincipal) as totaldue from rentals group by rentals.subid UNION select payments.payments_subid, - sum(principal) as totaldue from payments group by payments.payments_subid ] as tmp group by tmp.subid";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "$". $row['tmp']; }?>




Thanks.

Options: ReplyQuote


Subject
Views
Written By
Posted
Subtract 2 different tables
12015
May 09, 2007 09:35PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.