Jump to content

An UPDATE question


ririe44

Recommended Posts

This is a Mysql query I'm wanting to do... and here's my stab at it, and want to run it by you...

 

I have two tables: entries, and accounts.  A column in entries is called "cat", another called "sub", and another called "amount".  In the accounts table I have "cat", "sub", and "balance".  The accounts table is rows of individual "cat"s and "sub"s that the entries table uses. 

 

So, I want to subtract the sum of all "cat"/"sub" from the entries table from the "balance" column in the accounts table.

 

For example in entries there might be: 

Home - Electricity - $35

....

Home - Electricity - $35

.... etc.

 

I want to subtract $70 (35+35) from the account balance.

 

I only need to run this once, and it seems like a lot of code to do something simple... So, here's my stab at it:

 

$tbl_name = 'accounts';
$tbl_name2 = 'entries';
$sub_sum = mysql_query("SELECT SUM(amount) FROM $tbl_name2 WHERE `cat` = '' AND `sub` = ''") or die(mysql_error()); 
$sub_fetch = mysql_fetch_assoc($sub_sum);
$sub_total = $sub_fetch['SUM(budget)'];

mysql_query("UPDATE `accounts` SET `balance` = (`balance`- '$sub_total') WHERE `sub` = '$sub' AND `cat` = '$cat'") or die(mysql_error());

 

Now I'm not sure if it's even a stab... any help?

Link to comment
https://forums.phpfreaks.com/topic/156961-an-update-question/
Share on other sites

Maybe I should start simpler (for my sake)...  Let's say I just want to print all the distinct combinations of 'cat' and 'sub' from 'entries' with their totals....

 

$entry_data = mysql_query("SELECT SUM(amount) FROM $tbl_name WHERE ?__cat & sub are unique__?) or die(mysql_error());

Print "<th>Category:</th> <th>Sub-Category:</th> <th>Amount:</th></tr>";
while($info_entry = mysql_fetch_array( $entry_data ))
{
Print "<tr>";
Print "<td>".$info_entry['cat'] . "</td> ";
Print "<td>".$info_entry['sub'] . "</td> ";
Print "<td>$".$info_entry['amount'] . "</td> ";
}
Print "</table>";

Link to comment
https://forums.phpfreaks.com/topic/156961-an-update-question/#findComment-826799
Share on other sites

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.