Jump to content

Difference between results on two tables


richrock

Recommended Posts

Help!!!  I'm currently writing an admin panel, and need to extract two results from two tables and calculate a difference between the two.

 

The layout is :

1: The sum of: tbl_bids, row_bidvalue.  2: The sum of: tbl_sell, row_sold.

 

I can get the individual values using this:

 

// Get the auction price values
$query_sell = "SELECT userid, SUM(initial_price) FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid";
$result_sell = mysql_query($query_sell) or die(mysql_error());

// Get the bidded values
$query_bid = "SELECT userid, SUM(bid_price) FROM jos_bids WHERE userid = ".$uid." GROUP BY userid";
$result_bid = mysql_query($query_bid) or die(mysql_error());

 

But can't for the life of me figure out how to get a difference from each result.  I tried placing these in functions and call the functions as part of a sum :

 

$first_res = $result1;
$second_res = $result2;

echo "here";
echo $first_res;

$diff_total = $first_res - $second_res;

echo $diff_total;

 

But it doesn't work.  Sorry but I am a newbie at this.  I've only started doing PHP and instead of 'Hello World' I get an admin panel to code  ;D

 

Rich

Link to comment
https://forums.phpfreaks.com/topic/99349-difference-between-results-on-two-tables/
Share on other sites

Not really math based but whatever.

Try this:

 

<?php

// Get the auction price values
$query_sell = "SELECT userid, SUM(initial_price) FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid";
$result_sell = mysql_query($query_sell) or die(mysql_error());
list($sell_sum) = mysql_fetch_assoc($result_sell);

// Get the bidded values
$query_bid = "SELECT userid, SUM(bid_price) FROM jos_bids WHERE userid = ".$uid." GROUP BY userid";
$result_bid = mysql_query($query_bid) or die(mysql_error());
list($bid_sum) = mysql_fetch_assoc($result_bid);

echo "{$sell_sum} -  {$bid_sum}  =  ".($sell_sum - $bid_sum);

?>

 

Orio.

If you are using list(), you need an indexed array (ie mysql_fetch_row)

 

Really? I didn't know that...

 

So if that's the case, you can do it like this:

 

<?php

// Get the auction price values
$query_sell = "SELECT userid, SUM(initial_price) AS s1 FROM jos_bid_auctions WHERE userid = ".$uid." GROUP BY userid";
$result_sell = mysql_query($query_sell) or die(mysql_error());
$tmp = mysql_fetch_assoc($result_sell);
$sell_sum = $tmp['s1'];

// Get the bidded values
$query_bid = "SELECT userid, SUM(bid_price) AS s2 FROM jos_bids WHERE userid = ".$uid." GROUP BY userid";
$result_bid = mysql_query($query_bid) or die(mysql_error());
$tmp = mysql_fetch_assoc($result_bid);
$bid_sum = $tmp['s2'];

echo "{$sell_sum} -  {$bid_sum}  =  ".($sell_sum - $bid_sum);

?>

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.