Search code examples
phpmysqlcron-task

Performing a Mass Equation Update on MySQL Tables


There are a thousand ways I have seen to update all the rows on the Table to the same value, but how do I edit them all at once based on an equation?

Basically I have a cron running that says every hour run a php, the php does nothing as of yet..

I am curious as to how I would go about telling it to take each row and add to the field 'money' by the field 'revenue' and it would do that for each row individually instead of adding all the same value to all of them. I am curious as to how I would go about doing that.

(Example Code welcome lol) I have already tried a few ways but nothing seems to work, I just keep getting the effect of all the tables updating to the same value. When they should update relevant to each individual rows Money and Revenue fields.

Thanks in Advance! ~Blake Gillman


Solution

  • You probably need to reference the rows of the table in a update onto themselves. Something like:

    UPDATE your_table SET money = ( ( money + money ) / revenue ) WHERE 1;
    

    This will set the rows based on the values in that row itself. This is just an example, and you can add more complex formulas as you need. Check out the Math page of the MySQL docs. Good Luck.