I’m not sure this is even possible, but I would really like to be sure in order to create the most efficient code…
I need to build a query that works like an affiliate –
when a user signs in, I need to see if someone invited him, or if he got there alone (basic URL param). If he was invited, I need to give him X points. If the inviter was invited by someone, I need to give him 1/2X points, and if he was also invited, I need to give his inviter 1/4 points. I need this to be endless until the parent of all has no more “invited_by” values (null/0)…
I did this with a php (while x), a counter to calculate the amount of points – 1/(X sqr $i), but i’m not happy because it takes me a select and update query each time…
I’m using php and mysql.
Is there someone who can think of a better way to do this?
Thanks!
I think that you will need an iterative solution. I don’t see any hierarchical query operators in the MySQL manual that might help you out (but that isn’t quite the same as saying there aren’t any; my eyesight isn’t perfect). You could perform it in a stored procedure, which would reduce the cost of the operation (less data transferred between database server and client).
Also, the X/2, X/4, X/8, sequence isn’t captured by the expressions
1/2Xor1/(X sqr $i); I assume that there was no intent to give the invitrr a fraction of a point whereas the inviter’s inviter gets X/4 points?