Jump to content


Photo

Double Query


  • Please log in to reply
3 replies to this topic

#1 bearruler

bearruler
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 30 August 2006 - 02:31 PM

I'm not exactly sure how to ask this, so I'm going to try to give an example

I have an operation that will be run by cron, so I don't want it to be too heavy
As of now, It loops though everyone

Each time the file is run, the "Employees" gets 100% of the bosses income, divided amoung themselves. The bosses income does not include the bonus he gets from HIS boss.

So
































employee boss income money
10000 0 100000 0
10001 10000 50000 0
10002 10001 10000 0
10003 10001 5000 0



0 means they have no boss
And of course, this would never happen in a real world senario.

Without the boss bonus, it is a simple
$sql="Update `table` SET `money`=`money`+`income`";
However, I need to give the people with bosses x% of their bosses income

Employee 10000 gets his striaght income because he has no boss
Employee 10001 gets his income (50,000) PLUS his boss bonus. Since 10001 is the only one whos boss is 10000, he gets 100% (100,000). So he gets a total of 150,000
Employee 10002 gets his income (10,000) Plus his boss bonus. This time, there are two people who split it. He gets 50,000 / 2 = 25,000. His total is 35,000
Employee 10003 is the same as above, except he gets only 5,000 plus his 25,000 bonus.

I'm not really sure if this is possible, as it becomes multiple queries within a query....
Thanks,


Bear

www.KingdomsDivided.com

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 August 2006 - 02:59 PM

There are a number of ways to do this -- you can do it iteratively, but it might be easier to compile all this information in middleware, do the math there, and then run one update statement per employee afterwards.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 bearruler

bearruler
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 31 August 2006 - 12:28 AM

Which would put less load on the server?


Bear
www.KingdomsDivided.com

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 31 August 2006 - 03:56 AM

Well, which server? The DB server has less work to do if you get it all back, do the work, then update as required, but then the bulk of the work is still on the server-side script.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users