简体   繁体   中英

MySQL ORDER BY with two columns summed

I am creating a page where rows are displayed. Each row has two columns that are the point of my question, rating and usage. When the page is first loaded, there is no presorting and the page displays just fine. Code for when it is working:

if (!$sortby) {

    $getdata = mysql_query("select * FROM traditional") or die(mysql_error());

    while($data = mysql_fetch_array($getdata)){

        $category = $data["category"];
        $overview = $data["overview"];
        $ratingcount = $data["ratingcount"];
        $ratingscore = $data["ratingscore"];
        $usage = $data["usage"];
        $location = $data["location"];
        $calculaterating = $ratingscore / $ratingcount;
        if (!$calculaterating) {
            $rating = "None";
        } else {
            $rating = $calculaterating;
        }

        echo "</tr>";
        echo "<tr colspan='4'>";
        echo "<td class='selectcategory' width='40%'>";
        echo "<a href='$location'>$category</a>";
        echo "</td>";
        echo "<td class='categorymenu' style='vertical-align: middle; text-align: left;   
        padding: 5px;' width='40%'>";
        echo $overview;
        echo "</td>";
        echo "<td class='categorymenu' width='10%'>";
        echo $rating;
        echo "</td>";
        echo "<td class='categorymenu' width='10%'>";
        echo $usage;
        echo "</td>";
    }

Here's where things stop working. There is a drop down menu on the page that allows the user to sort by usage or rating. When they choose one the page refreshes and $sortby is populated by either rating or usage. Here's the rest of the code, when things stop working:

} else {

    if ($sortby = "rating") {
        $sort = "rating";
    } else {
        $sort = "`usage`";
    }
    $getdata = mysql_query("select category,overview,SUM(ratingscore / ratingcount) as   
'rating',`usage`,location FROM traditional ORDER BY $sort DESC") or die(mysql_error());

    while($data = mysql_fetch_array($getdata)){

        $category = $data["category"];
        $overview = $data["overview"];
        $rating = $data["rating"];
        $usage = $data["usage"];
        $location = $data["location"];

        echo "</tr>";
        echo "<tr colspan='4'>";
        echo "<td class='selectcategory' width='40%'>";
        echo "<a href='$location'>$category</a>";
        echo "</td>";
        echo "<td class='categorymenu' style='vertical-align: middle; text-align: left;   
        padding: 5px;' width='40%'>";
        echo $overview;
        echo "</td>";
        echo "<td class='categorymenu' width='10%'>";
        echo $rating;
        echo "</td>";
        echo "<td class='categorymenu' width='10%'>";
        echo $usage;
        echo "</td>";
    }
}

When $sortby is not populated, about 10 rows display. With the code that isn't working (when one attempts to sort the results), only one row displays (don't know why?) and the one rating row that shows up isn't calculated right anyway. I have read a bunch of other posts that sort of deal with this (which kind of helped me craft the bad code as I currently have it). But I guess I'm just not understanding from what I'm getting from others' posts.

更好的是,您可以进行其他查询...,以便可以使用简单的ORDER BY查询..并回显查询生成的输出。

SUM is an aggregate operator and using it without GROUP BY will make your whole result set collapse into one row where result is set to the sum of the results of all rows.

I'm not sure why you're using SUM at all, if you want to have the same rows as in the first query with just ordering added, it should be something like;

SELECT category, overview, ratingscore / ratingcount AS rating,
    `usage`, location FROM traditional ORDER BY $sort DESC

If I'm misunderstanding your desired result set, let me know, but SUM without a GROUP BY is definitely not what you want in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM