Page 1 of 1
MySQL math?
Posted: Sat Feb 12, 2011 11:22 am
by Domsore
So I have a table like:
Games Won | Games Lost | XP
11 | 3 |
but is there a way to do like math in MySQL so XP = (Games Won X 10) - (Games Lost X -5)
or does it have to be done in php then updated?
Cheers
Re: MySQL math?
Posted: Sat Feb 12, 2011 11:27 am
by Darhazer
update `table` set `xp` = (games_won * 10) - (games_lost * 5)
Re: MySQL math?
Posted: Sat Feb 12, 2011 8:38 pm
by califdon
As Darhazer said, you can include simple computation in SQL statements, but be warned that in most circumstances you should be storing raw data in a database and making calculations when you retrieve the data. For a particular, very specific application, it will often work to store these calculated values, but for more useful databases, you usually will not be able to predict what you or someone else may want to do with the data in the future, so rather than storing manipulated data, you should nearly always design it to store the basic raw data. You can always apply whatever factors you want when you retrieve the data later, but the real original data is what will remain stored in the database.
Re: MySQL math?
Posted: Sun Feb 13, 2011 6:52 am
by Domsore
Appreciate the help and tips, thanks
Dom