Jump to content

Archived

This topic is now archived and is closed to further replies.

bearruler

Double Query

Recommended Posts

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

[table]
[tr]
[td]employee[/td]
[td]boss[/td]
[td]income[/td]
[td]money[/td]
[/tr]
[tr]
[td]10000[/td]
[td]0[/td]
[td]100000[/td]
[td]0[/td]
[/tr]
[tr]
[td]10001[/td]
[td]10000[/td]
[td]50000[/td]
[td]0[/td]
[/tr]
[tr]
[td]10002[/td]
[td]10001[/td]
[td]10000[/td]
[td]0[/td]
[/tr]
[tr]
[td]10003[/td]
[td]10001[/td]
[td]5000[/td]
[td]0[/td]
[/tr]
[/table]


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
[code]$sql="Update `table` SET `money`=`money`+`income`";[/code]
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.