PHP使用两个表结果排序值

时间:2011-07-24 01:35:20

标签: php mysql

我正在尝试使用ORDER BY DESC对结果进行排序,但结果按foreach值排序:

while($row = mysqli_fetch_array($sqlgroup)){ 
    $member_array = $row["member_array"];

    if ($member_array !=""){
        $memberArray = explode(",", $member_array);
        $i = 0;
        $cashstatsList .= '
        ';
        foreach($memberArray as $gkey => $mvalue){
            $i++;

            $arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg,
                            SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg
                            FROM player, groupcash
                            WHERE (player.id = grpcsh_plrid) AND (player.id = $mvalue) AND (groupcash.grpcsh_groupid = $groupid)                                        
                            AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC                               
                            ";

             $arraystatsResutls = mysqli_query($link, $arraystats);
             if (!$arraystatsResutls){
                $cashstatsList .= '                         
                    <tr>
                        <td>                            
                            No results available for listed dates
                        </td>
                    </tr>';
            } else {
                while($row = mysqli_fetch_array($arraystatsResutls)){ 
                    $memberFirstName = $row["first_name"]; 
                    $memberLastName = $row["last_name"];
                    $sum = $row["memsum"];
                    $avg = $row["memavg"];
                    $win = $row["memcntpos"];
                    $loss = $row["memcntneg"];

                    if ($memberFirstName == "" || $memberLastName == ""){
                        $sqlName = mysqli_query($link, "SELECT first_name, last_name FROM player WHERE id='$mvalue' LIMIT 1") or die ("Sorry we had a mysql error!");
                            while ($row = mysqli_fetch_array($sqlName)) {
                                $memberFirstName = $row["first_name"]; $memberLastName = $row["last_name"];
                            }                                           
                    }

                    if ($sum == ""){
                        $sum = "0";
                    }
                    if ($avg == ""){
                        $avg = "0";
                    }
                    if ($win == ""){
                        $win = "0";
                    }
                    if ($loss == ""){
                        $loss = "0";
                    }

                    $cashstatsList .= '
                        <tr align="center">
                            <td>
                                ' . $i . '
                            </td>
                                <td>
                                ' . $memberFirstName . '&nbsp;' . $memberLastName . '
                            </td>
                            <td>
                                $' . $sum . '
                            </td>
                            <td>
                                $' . $avg . '
                            </td>
                            <td>
                                ' . $win . '/' . $loss . '
                            </td>
                        </tr>'; 
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在查询中一次选择该组的所有成员,以便它们可以是“ORDER BY”,这里您只有每个请求的同一成员的结果,因此订单不会按顺序更改任何内容成员之间的结果。

如果$member_array1,454,33,22类似,您可以删除foreach并使用查询:

$arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg,
                SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg
                FROM player, groupcash
                WHERE (player.id = grpcsh_plrid) AND (player.id IN ($member_array)) AND (groupcash.grpcsh_groupid = $groupid)                                        
                AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC                               
                ";

因此,您将同时获得该组的所有成员,并且ORDER BY将起作用。