473,486 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can I store the sum of row's child rows' field in a parent field?

4 New Member
Sorry if this has been posted before; I am not sure what to search for.

I have a table of users, `users`.
In the table `users` there is a row which stores clicks, `users`.`clicks`.
Each user has it's own referrer, `users`.`referrer_id` which refrences the referring user's id.
In the referring user's row, I would like a field to store referral clicks, `users`.`referral_clicks`.
I would like `users`.`referral_clicks` to be updated each time that a child user's `users`.`clicks` field is changed.

Is this easily possible in MySQL?

Is this more efficient than something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT SUM(`users`.`clicks`) FROM `users` WHERE `users`.`referrer_id` = 1;
as opposed to:
Expand|Select|Wrap|Line Numbers
  1. SELECT `users`.`referral_clicks`) FROM `users` WHERE `users`.`id` = 1;
Thanks,

Ben Manns
Nov 1 '08 #1
2 2628
Atli
5,058 Recognized Expert Expert
Hi.

As a general rule, it is best not to store the same piece of data twice.
You already have the information you need stored in the `user`.`clicks` field, so adding a second field isn't needed.

I would go with the first method you mentioned, rather than adding unnecessary data. I doubt that you will ever notice the added overhead of using the SUM function, if there even is any.

But, if you choose to add the second field, you could try using Triggers. That way you could have it automatically update the referrer_clicks field whenever a clicks field is modified.
Nov 1 '08 #2
benmanns
4 New Member
Thanks. It looks like those are what I am looking for.

I'm going to be storing individual transactions in which thousands of users can have anywhere between 5 and 100 per day. I am thinking that there might be some problem if I have to sum the transactions (up to 36,500 per user per year) every time I want to display or check their balance.

Would overhead still not be a problem, or am I correct in wanting to store the data in the parent user?
Nov 1 '08 #3

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

Similar topics

2
4373
by: Caroline | last post by:
I seem to always want to do this type of join, but I can never find how to do this. I am trying to join two tables. The parent table will always have one row of data and the child may have 0 to...
0
1223
by: Bruce | last post by:
I have three tables in SQL serve tblareas (w/autoincrement ID tblprograms (w/autoincrement ID tblphases (w/autoincrement ID I've build cascading relation from tblareas <one-many> tblprograms...
5
6181
by: mike | last post by:
I am adding a row to a table from a child window that opened from the parent window. My js looks like: if ( self.opener.document.update ) { var rows =...
1
3041
by: TusharP | last post by:
Hi Friends, My requirement are as follow..... 1) First DataGrid display all row of one table, with this one additional column having +/- control like tree. 2) When user click on +/- control ...
0
1702
by: Benny Raymond | last post by:
reply to: benny@pocketrocks.com if possible: I'm trying to set up a hierarchy system in this database where each row can be related to a previous row. The problem is that when I go to...
1
1613
by: Chris Fairman | last post by:
NG, I have a Dataset with multiple tables and relations defined. Currently when displaying a table from this Dataset in a DataGrid a hyper-link to the child rows is displayed. After clicking...
11
4346
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store...
1
1093
nev
by: nev | last post by:
Good day Sirs & Madams, I know some of you will find this problem very simple. Kindly share a little knowledge to me on how to deal with this. Thank you. I have a parent-child table relation. when...
29
5302
by: shivasusan | last post by:
Hi! I can add rows with inputs to my HTML table dynamically using DOM, but I cannot remove selected rows. In fact, every row contains a Delete button. So, user selects the rows to remove, clicks...
0
7123
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,...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4863
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
4559
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
3066
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
1378
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
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.