Hi, I have two rows, one holds the number of votes, and the other holds teh sum of the votes. How can I get the average of the two in a third row?
What language are you using on your site. You can't accomplish that with plain HTML. If your site is in php then you need to do the following <? $column1 = [number_of_votes]; $column2 = [sum_of_votes]; $column3 = ($column2/$column1); echo column3; ?> If you are using ASP then the following should do the trick <% dim column1 dim column2 dim column3 column1 = [number_of_votes] column2 = [sum_of_votes] column3 = (column2/column1) response.write column3 %> Hope this helps. I would be more specific if I knew more about your site. Blu
yes I realize I can't do it with HTML, that is why I posted it in the databases forum. My scripting language (PHP) is irrelevant, because I need ot do this with MySQL for sorting reasons
In that case you need to create a script (since stored procedures are not allowed on mySQL) that does this for you, and you run it once a day (or however often you need to get the averages.. Here is the structure you need to follow <? Connect to DB sql = SELECT column1, column2 from tblName execute the sql query average = (column2/column1) // get the average for each row in sql query execution $updatesql = "update tblName set column3 = average" execute updatesql loop through each result I don't know how good you are with php. Let me know if you need me to write a script. Blu
I could do it, but was hoping there is a better way Would you know of any other ways to do this: while ($data = mysql_fetch_array(mysql_query("SELECT votes, numvotes FROM my_tbl ORDER BY votes DESC LIMIT 100"))) { echo "$data[votes]"; } PHP: I want that to show the votes ordered by average instead of total as it is now.
unfortunately without having the "average" in a table field, I don't know of any other way. The best way I can think of is to create a php page, which would insert the average in a separate field. With SQL, you could create a stored procedure, which would update that automatically, as soon as there is a change on a table. Unfortunately with mySQL, I don't know of any other way. My appologies. Blu
thanks for the help. I guess I will go with my original idea, to have a third row created. and on data update, grap the existing vote count and vote total, and then divide them up, and insert the new average, then update the vote count and vote total