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.

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证