dongqijuan3786 2012-05-05 18:18
浏览 36

mysql或平均或者我应该使用PHP?

I am trying to find an average of an average with a mysql query.

I have 12 criteria and i can get an average for a column but i can not find the average of average,

Am trying to to work out a total rating of 12 criteria.

I think there are a few ways to go about this.

  1. is to divide the total score by the number votes, problem is each row has a possibility of 12 votes.
  2. get the avg totals, find our # of values set then divide the avg totals by # of values set

I need the number that i am dividing the total by to change depending on votes. 1 used can input 12 votes in 1 row 2 users could input a total of 18 votes in 2 rows, which means 1 user has not completed 6 votestable data The table has been shortened for testing purposes to columns that contain votes. I have posted this already, sorry for repost, but am starting to understand what help i roughly need

  • 写回答

2条回答 默认 最新

  • doukanhua0752 2012-05-05 18:48
    关注

    First, in order to decide whether a user has voted or not, you need to have something other than an integer stored in the field until a vote is added. I would suggest NULL.

    Next, add another column to the table for the user's average rating. This would be calculated using the columns in the row that don't have a null value. Example semi-pseudo code:

    // Get users row from database
    // Use a query like: 
    // SELECT comfort,service,friendliness,food,drinks,toilet,music FROM myTable WHERE user_id = 123;
    $row = $db->fetchrow($userid);
    
    // Loop through and find out how many are valid
    // Also add values together
    foreach($row as $col)
    {
       if( !is_null($col) )
       {
          $numValidColumns++;
          $totalVote = $totalVote + $col; 
       }
    }
    
    // Calculate user's average
    $userAvg = $totalVote/$numValidColumns;
    
    // Add to db for this row in new field you just made
    $db->insert(...);
    

    Something like this would allow you to calculate the user's average save it. If they add another vote, you would recalculate this average.

    The final step is to query all the rows and add the values of the user average column together. Divide it by the total number of rows to arrive at an overall average.

    Hope that is on the right track for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上