$db->query("UPDATE users SET uOffensiveMen=uOffensiveMen-(uOffensiveMen*0.10) WHERE uRace=1 AND uOffensiveMen>10");
I start with the value of 20 in my table and it subtracts 2 the first time, but then when I run it again it still subtracts 2, and run again it will only subtract 2 again over and over until the value is below 10.
What am I doing wrong?
Last edited by netpants on Wed Feb 07, 2007 9:45 am, edited 1 time in total.
Sounds like a data type issue. It looks like MySQL is doing integer math instead of using floats/decimals. If you intend to subtract a rounded whole number every time, then I think your formula is correct. For example, 10% of 18 = 1.8, which rounds to 2 when it is automatically cast as an integer.
However, if you want to use floating point numbers, then you should first confirm that "uOffensiveMen" in the table is set to the correct data type, something like decimal(10,2). MySQL will evaluate the expression based on the field type.
I agree. You should change your data type to decimal or some other floating point type data type so when the math is calculated it is not made into an integer.
Well it does work fine the first time, but if I subtract 10% from 12, it still takes two away, or is it always going to round up, and should I try starting out with a higher number and see what it does.
Ok I booted it up to 200 and did the query and it took 20 the first time, then 18 the second time, so it appears to be working all this time lol. Thanks for your help and insight.