Mysql按评级排序 - 但捕获全部

时间:2009-03-28 11:12:24

标签: mysql inner-join rating

我有这个PHP / MYSQL代码,它返回按照评级排序的表中的记录,从最高评级到最低评级:

<table width="95%">
    <tr>
    <?php
        if (isset($_GET['p'])) {
            $current_page = $_GET['p'];
        } else {
            $current_page = 1;
        }
        $cur_category = $_GET['category'];
        $jokes_per_page = 40;
        $offset = ($current_page - 1) * $jokes_per_page;

        $result = mysql_query("
        select jokedata.id as joke_id, jokedata.datesubmitted as datesubmitted,
        jokedata.joketitle as joke_title, sum(ratings.rating)/count(ratings.rating) as average
        from jokedata inner join ratings
        on ratings.content_type = 'joke' and ratings.relative_id = jokedata.id
        WHERE jokecategory = '$cur_category'
        group by jokedata.id
        order by average desc
        limit $offset, $jokes_per_page
        ");

        $cell = 1;
        while ($row = mysql_fetch_array($result)) {
            if ($cell == 5) {
                echo "</tr><tr class=\"rowpadding\"><td></td></tr><tr>";
                $cell = 1;
            }
            $joke_id = $row['joke_id'];
            $joke_title = $row['joke_title'];
            $joke_average = round($row['average'], 2);

            echo "<td><strong><a class=\"joke_a\" href=\"viewjoke.php?id=$joke_id\">$joke_title</a></strong> -average rating $joke_average.</td>";
            $cell++;
        }
    ?>
    </tr>
    <tr class="rowpadding"><td></td></tr>
</table>

它完美无缺,但有一个问题 - 如果一个项目没有至少一个评级,则查询根本不会选择它!

我该如何解决这个问题?感谢。

4 个答案:

答案 0 :(得分:1)

您需要使用左连接而不是内连接,然后处理ratings.ratings为null的情况:

$result = mysql_query("
            SELECT jokedata.id AS joke_id, 
            jokedata.datesubmitted AS datesubmitted,
            jokedata.joketitle AS joke_title, 
            -- average is 0 if count or sum is null
            IFNULL(SUM(ratings.rating)/COUNT(ratings.rating), 0) AS average
            FROM jokedata 
            -- return all rows from left table (jokedata), and all nulls for ratings
            -- data when there is no matching row in the right table (ratings)
            LEFT JOIN ratings ON ratings.content_type = 'joke' AND jokedata.id = ratings.relative_id 
            WHERE jokecategory = '$cur_category'
            GROUP BY jokedata.id
            ORDER BY average desc
            LIMIT $offset, $jokes_per_page
            ");

左连接将返回jokedata的所有结果,并且只返回未满足连接条件的每一行的评级的所有空值。

答案 1 :(得分:1)

我会推荐以下内容:

  • 使用left outer join来获取没有评分的笑话
  • 使用avg()代替手动计算平均值
  • 可能会使用coalesce()来避免结果中的null

以下是表格的简化版本:

create table joke(jokeid int primary key, jokedata varchar(50));
create table ratings(rating int, relative_id int);
insert into joke values(1, "killing");
insert into joke values(2, "no rating");
insert into ratings values(5, 1);
insert into ratings values(10, 1);

一些示例查询:

select joke.jokeid, avg(ratings.rating) as average 
   from joke 
   left outer join ratings 
     on ratings.relative_id = joke.jokeid 
   group by joke.jokeid;
+--------+---------+
| jokeid | average |
+--------+---------+
|      1 |  7.5000 | 
|      2 |    NULL | 
+--------+---------+

或者,使用coalesce()

select joke.jokeid, avg(coalesce(ratings.rating, 0)) as average 
  from joke 
  left outer join ratings 
    on ratings.relative_id = joke.jokeid 
  group by joke.jokeid;
+--------+---------+
| jokeid | average |
+--------+---------+
|      1 |  7.5000 | 
|      2 |  0.0000 | 
+--------+---------+

答案 2 :(得分:1)

试试这个:

SELECT  jokedata.id as joke_id,
        jokedata.datesubmitted as datesubmitted,
        jokedata.joketitle as joke_title,
        COALESCE(
        (
        SELECT  AVG(rating)
        FROM    ratings
        WHERE   ratings.relative_id = jokedata.id
                AND ratings.content_type = 'joke'
        ), 0) AS average
FROM    jokedata
ORDER BY
        average DESC
LIMIE   $offset, $jokes_per_page

答案 3 :(得分:0)

在我看来,使用平均值有点不公平。如果Joke A的评分为1,而Joke B的评分为25,那么Joke A的排名将高于Joke B.这使得不受欢迎的笑话更有利于排名更高。

我建议将权重与评分相关联,然后按权重排名。例如,在1到5的范围内,1将获得-2,2将获得-.5,3是0,4是+5,5将获得+2。因此,这将允许5“4”评级的笑话排名高于1“5”评级的笑话。

-2到+2比例可能需要一些调整,但希望你看到我的观点。