473,508 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

permanently store balance result in a field of current table

1 New Member
I have created this table:

Expand|Select|Wrap|Line Numbers
  1. mysql> describe acount;
  2. +----------+---------------+------+-----+---------+----------------+
  3. | Field    | Type          | Null | Key | Default | Extra          |
  4. +----------+---------------+------+-----+---------+----------------+
  5. | uniqueID | bigint(20)    | NO   | PRI | NULL    | auto_increment |
  6. | amount   | decimal(20,0) | NO   |     | NULL    |                |
  7. | BALANCE  | decimal(20,2) | NO   |     | 0.00    |                |
  8. +----------+---------------+------+-----+---------+----------------+
  9. 3 rows in set (0.04 sec)
and this are my current table:
Expand|Select|Wrap|Line Numbers
  1. mysql> select * from acount;
  2. +----------+--------+---------+
  3. | uniqueID | amount | BALANCE |
  4. +----------+--------+---------+
  5. |        8 |    200 |    0.00 |
  6. |        9 |    100 |    0.00 |
  7. |       10 |    200 |    0.00 |
  8. |       11 |      5 |    0.00 |
  9. |       12 |     13 |    0.00 |
  10. |       13 |    145 |    0.00 |
  11. |       14 |   -100 |    0.00 |
  12. |       15 |    125 |    0.00 |
  13. |       16 |     15 |    0.00 |
  14. +----------+--------+---------+
  15. 9 rows in set (0.00 sec)
The problem is in the BALANCE field.It does no return any result.
When i enter this command:
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT uniqueID, amount, amount + coalesce( (
  2.     ->
  3.     -> SELECT sum( amount )
  4.     -> FROM acount b
  5.     -> WHERE b.uniqueID < a.uniqueID ) , 0
  6.     -> ) AS BALANCE
  7.     -> FROM acount a;
  8.  
It will produce a table like this:
Expand|Select|Wrap|Line Numbers
  1. +----------+--------+---------+
  2. | uniqueID | amount | BALANCE |
  3. +----------+--------+---------+
  4. |        8 |    200 |     200 |
  5. |        9 |    100 |     300 |
  6. |       10 |    200 |     500 |
  7. |       11 |      5 |     505 |
  8. |       12 |     13 |     518 |
  9. |       13 |    145 |     663 |
  10. |       14 |   -100 |     563 |
  11. |       15 |    125 |     688 |
  12. |       16 |     15 |     703 |
  13. +----------+--------+---------+
  14. 9 rows in set (0.00 sec)
But I need the BALANCE table to permanently store the BALANCE result as table above.
when i check the balance field again, the result return to default value.
Expand|Select|Wrap|Line Numbers
  1. mysql> select balance from acount;
  2. +---------+
  3. | balance |
  4. +---------+
  5. |    0.00 |
  6. |    0.00 |
  7. |    0.00 |
  8. |    0.00 |
  9. |    0.00 |
  10. |    0.00 |
  11. |    0.00 |
  12. |    0.00 |
  13. |    0.00 |
  14. +---------+
  15. 9 rows in set (0.00 sec
I need the balance to be update, automaticaly calculated and stored permanantly in the current table.
Just as table below:
Expand|Select|Wrap|Line Numbers
  1. +----------+--------+---------+
  2. | uniqueID | amount | BALANCE |
  3. +----------+--------+---------+
  4. |        8 |    200 |     200 |
  5. |        9 |    100 |     300 |
  6. |       10 |    200 |     500 |
  7. |       11 |      5 |     505 |
  8. |       12 |     13 |     518 |
  9. |       13 |    145 |     663 |
  10. |       14 |   -100 |     563 |
  11. |       15 |    125 |     688 |
  12. |       16 |     15 |     703 |
  13. +----------+--------+---------+
Can someone help me in this. Im totaly new in Mysql database.
Oct 2 '09 #1
1 2527
Atli
5,058 Recognized Expert Expert
Hey.

If you want to change the value of a field you need to use the UPDATE command.

The problem is that you can't run a sub-query from within an UPDATE command on the table being updated, which makes it very difficult to calculate the balance correctly using the UPDATE command alone.

Best I can come up with at the moment is creating a procedure that calculates this for you and use that to get the value for the balance field.
Expand|Select|Wrap|Line Numbers
  1. UPDATE myTable
  2.     SET `balance` = GetBalance(`id`);
Where "GetBalance" is a function you create to calculate the balance.
(Check this out if you are new to procedures.)



You can avoid this problem altogether tho by adding this to the row when it is created.

The INSERT command can use the result set of a SELECT command as the values that are supposed to be inserted. You can use this to set the "balance" field of your new row as you INSERT it.
Like:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `myTable`(`amount`, `balance`)
  2. SELECT 10.00, IFNULL(SUM(`amount`) + 10.00, 10.00) FROM `myTable`;
Oct 4 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1538
by: Ethan | last post by:
I've been having some really weird problems with a very simple PHP app. I'm wondering if anyone can help me sort this out. I have a page that prints out the results of a MySQL query as an HTML...
5
6760
by: bobdydd | last post by:
Hi Guys Can anyone help I am trying to find a way to implement a running balance that will re-calculate if the date order is changed, or if an earlier record is changed like you might want to do...
0
2204
by: Harley | last post by:
I am trying to write a personal app to keep a bank balance and history. The problem I'm haveing is finding a decent way to store the data on a pocketpc under .net compact framewok useing vb.net....
2
6769
by: Prakash | last post by:
I have 2 tables ... Customer_Master: Cust-Code, Cust_Name Customer_Transactions: Cust_Code, Date, Details, Debit, Credit I would like to generate a report in the foll manner, say from...
1
2129
by: sjvandevoorde | last post by:
I need some help with a couple forms/test fields. Bare with me I have been out of the field for some time and only consider myself an intermediate user. The first one is a subfrom which lists...
5
1811
by: eric.nguyen312 | last post by:
I have an amend button which when clicked puts the Job form into edit. When saved, Access backs up old job information into 'AmendedJobBackUp' table. What I want is to add a new column...
1
3355
by: assgar | last post by:
Hi I was using a schroll bar to display multiple rows of dynamically created from database records. The scrolling was not displaying the data properly so I have decided to use pagination. The...
3
5158
by: Paul H | last post by:
I have a transactions table and a balance table that look something like this: tblTransactions TransactionID (PK Autonumber) ClientID TransactionDate TransactionAmount (currency field, values...
1
3009
maxx233
by: maxx233 | last post by:
I need to figure out a way to generate a report to find customers in our DB that have unused credits on their account so we can void them out periodically - we manually assign them credits, they make...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.