Connecting Tech Pros Worldwide Help | Site Map

MySQL 2 stage queries

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#1: Feb 5 '09
Hi guys,
What I have is a tree link relationship between users on my site. It is kind of like a pyramid scheme in that if someone joins "under" you, when they get a daily update, you get 20% of their daily update.

I have a MySQL statement to this effect:
Expand|Select|Wrap|Line Numbers
  1. UPDATE users SET value=(value*factor+value)
Now to update receruiters (superior) could I have something like:
Expand|Select|Wrap|Line Numbers
  1. $rows = mysql_query("SELECT name, superior, value, factor FROM users");
  2.  
  3. while ($rows) {
  4. $row = msql_fetch_array($rows);
  5. $name = $row['name'];
  6. $superior = $row['superior'];
  7. $new_value = $row['value'] * $row['factor'] + $row['value'];
  8. $sups_value = $new_value * 0.2;
  9. mysql_query("UPDATE users SET value='$new_value' WHERE name=$name");
  10. mysql_query("UPDATE users SET value='$sups_value' WHERE name=$superior");
  11. }
However the problem is this means that there will be two UPDATEs for every row found and I was wondering if there is an easier way that anyone knows of?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Feb 5 '09

re: MySQL 2 stage queries


This link may be of help:

http://forums.devshed.com/mysql-help...ry-489004.html
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#3: Feb 5 '09

re: MySQL 2 stage queries


Cheers, some things to make my while loop slightly faster, but is there way of doing this without a while loop? As in just UPDATE-ing instead of SELECTing processing and UPDATE-ing?
Reply