MySQL math?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

MySQL math?

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: MySQL math?

Post by Darhazer »

update `table` set `xp` = (games_won * 10) - (games_lost * 5)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: MySQL math?

Post 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.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: MySQL math?

Post by Domsore »

Appreciate the help and tips, thanks

Dom
Post Reply