Mysql Arithmetic..

admin

Administrator
Staff member
Hello fellas...I need some clarification on something. <br /><br />Ok...i'm not clear on mysql float datatypes. I mean, my script is working but I would like to know exactly what its doing. <br /><br />I'm running a particular query...<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$sql = "UPDATE table SET votes=votes+1, votecount=votecount+$rating, rating=votecount/votes WHERE linkid=$id";<!--c2--></div><!--ec2--><br /><b>mysql datatypes are as follows</b><br />votes (int)<br />votecount (int)<br />rating (float)<br /><br />the part i'm unclear about is the division i used here "rating=votecount/votes"<br />I read up a little at mysql.com and it discusses <a href="http://www.mysql.com/doc/en/Arithmetic_functions.html" target="_blank">some</a> arithmitic operations, but not division... so i'm dividing 2 integers and I want to know what type of division mysql does (percision wise).<br /><br />This is what i'm getting pushed into the database..Ex. (29/6) = <b>4.83333 </b><br /><br />You would think since its a repeating decimal, it would push the maximum length of numbers, but that isn't the case.<!--content-->
Hi section31,<br /><br />I haven't done a lot of MySQL programming, but I've been programming object oriented languages such as C++ and Java for several years. In those languages, when I came upon a dilemma such as this, I would simply cast the resultant value into whatever data type form I wanted.<br /><br />For instance, consider the following line of code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->int x;<br />int y =10;<br />int z = 3;<br /><br />// If left uncast, the following line would give some sort of error because y/z is not an integer. The actual value is a never-ending decimal 3.333......<br />x = y/z;<br /><br />// However, if I cast the result as an integer (to match the data type of "x"), the line will execute smoothly, and give the result of x=3<br />x = (int)y/z;  // Casts 10/3 as an integer before assigning the value to variable x.<!--c2--></div><!--ec2--><br /><br />I don't know if casting is an option in MySQL, but it was worth a mention!<br /><br />Good luck!<br /> <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/goof.gif" style="vertical-align:middle" emoid=":goof:" border="0" alt="goof.gif" /><!--content-->
nah..i don't think casting is an option. <br /><br />thanks anyway, <br />dave<!--content-->
If you're concerned about the decimal, you can use the MySQL ROUND(). Check out the <a href="http://www.mysql.com/doc/en/Mathematical_functions.html" target="_blank">Mathematical functions</a>.<!--content-->
<!--QuoteBegin-btrfld+Mar 31 2004, 08:58 PM--><div class='quotetop'>QUOTE(btrfld @ Mar 31 2004, 08:58 PM)</div><div class='quotemain'><!--QuoteEBegin-->If you're concerned about the decimal, you can use the MySQL ROUND(). Check out the <a href="http://www.mysql.com/doc/en/Mathematical_functions.html" target="_blank">Mathematical functions</a>.<!--QuoteEnd--></div><!--QuoteEEnd--><br /> No i wanted a decimal...just unsure why it rounds to the 6th decimal place...<br /><br />Oh...another quick question if anyone can help..anyone know which is faster...mysql LOCATE function or LIKE.<!--content-->
 
Back
Top