dougutuo9879 2013-07-16 19:12
浏览 105
已采纳

寻找中位数Mysql

Trying to find the median for an array in a MYSQL database.

Currently, I am grabbing my data like so:

$middleMonth = "SELECT Day, COUNT(Day) AS totalNumber FROM finalbaby GROUP BY Day ORDER BY COUNT(Day) DESC LIMIT 1, 300";
$middleResult = mysql_query($middleMonth);

I am then putting it into an array like so.

$names=array();
while($row = mysql_fetch_assoc($middleResult)) {
$names[] = $row['Day'];

I am then trying to find the median of that array:

            sort($names);
            $count = count($names);
            $middleval = floor(($count-1)/2); 
                if($count % 2) { 
                    $median = $names[$middleval];
                } else { 
                    $low = $names[$middleval];
                    $high = $names[$middleval+1];
                    $median = (($low+$high)/2);
                }

        return $median;

        }

        var_dump($names);

I am not getting any errors, but it crashes my application.

Any suggestions on what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongyunshan4066 2013-07-16 19:52
    关注

    are you closing the while in your second code black before your third code block?

    answered:

    I close the while in my last }

    Fail... close while in second code block before running code in third code block... this will find the median only once instead of trying to find the median every time you set a name from a mysql returned row. The first time, the count of names would be 1 and then you subtract 1 = 0 and divide by two. This will give you 0. Then you try to get the remainder of 0 and 2. This throws off the rest of your code. Try this:

    $middleMonth = "SELECT Day, COUNT(Day) AS totalNumber FROM finalbaby GROUP BY Day ORDER BY COUNT(Day) DESC LIMIT 1, 300";
    $middleResult = mysql_query($middleMonth);
    $names=array();
    while($row = mysql_fetch_assoc($middleResult)) {
        $names[] = $row['Day'];
    }
    sort($names);
    $count = count($names);
    $middleval = floor(($count-1)/2); 
    if($count % 2) { 
        $median = $names[$middleval];
    } else { 
        $low = $names[$middleval];
        $high = $names[$middleval+1];
        $median = (($low+$high)/2);
    }
    echo $median;
    var_dump($names);
    
    //previous code
    //if this code is inside function then return if not then print
    //return $median;
    //if this code is inside function then the return above will cancel this var_dump
    //var_dump($names);
    

    Also please remember

    Warning! This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。