MySQL - 计算相关记录的百分比和计数

时间:2016-02-20 21:42:38

标签: mysql sql count average

请完成代码以完成以下操作:

计算特定记录的投票数和百分比,是一个电影网站,用户可以查看和投票电影,那么网站应该显示所有电影更多,以降低用户的投票数量,这里有详细的内容我试图这样做。

1)计算每部电影的投票数。

2)单独获得投票数"他们共3个表[投票] "对于每部电影,例如:

电影1共有100张选票,20%的用户似乎常规,30%的用户似乎良好,50%的用户似乎很好

3)获取表格[选票]和#34;之间" 3之间的最大投票数百分比,如果用户投票更多的电影非常好,我有计算它的百分比,这样我们就可以创建一个小的甜甜圈或图形,比用户更喜欢显示电影和演员表,例如:

这部电影被评选为电影1,共有450票,占用户评分所有票数的50%。

到目前为止,我已经这样做了:

success: function() {
    window.alert('success!!');
    check = true;
    st_bookmark = [];
    str_print = [];
    end_print = [];
    ed_bookmark = [];
}

图表数据库:

enter image description here

整个数据库的完整代码:

SELECT
    M.name AS title_movie,
    M.year AS premiere,
    G.name AS gender,
    COUNT(MHR.vote_id) AS total_voting_movie
FROM movies M
LEFT JOIN gender G ON M.gender_id = G.id
LEFT JOIN movies_has_rating MHR ON M.id = MHR.movie_id
GROUP BY M.id
ORDER BY M.id DESC

非常感谢您的支持,非常感谢。

2 个答案:

答案 0 :(得分:2)

你的第三部分不清楚。但第二部分可以通过以下方式解决:

SELECT
    M.name AS title_movie,
    M.year AS premiere,
    G.name AS gender,
    COUNT(MHR.vote_id) AS total_voting,
    -- 2)
    COUNT(MHR.vote_id = 1 OR NULL) AS count_very_good,
    COUNT(MHR.vote_id = 2 OR NULL) AS count_good,
    COUNT(MHR.vote_id = 3 OR NULL) AS count_regular
FROM movies M
LEFT JOIN gender G ON M.gender_id = G.id
LEFT JOIN movies_has_rating MHR ON M.id = MHR.movie_id
GROUP BY M.id
ORDER BY M.id DESC

http://sqlfiddle.com/#!9/5ba403/5

我确定你知道如何计算该结果的百分比。

更新:因此,OP要求确定具有最高计数的投票类型,并计算其在SQL中的优势。您可以(但不应该)使用以下查询来执行此操作:

SELECT t.*, 
    CASE
        WHEN highest_votes_id = 1
            THEN 100 * count_very_good / total_voting
        WHEN highest_votes_id = 2
            THEN 100 * count_good / total_voting
        WHEN highest_votes_id = 3
            THEN 100 * count_regular / total_voting
    END AS highest_votes_percentage
FROM (  
    SELECT t.*,
        CASE
            WHEN count_regular > count_good AND count_regular > count_very_good
                THEN 3
            WHEN count_good > count_very_good
                THEN 2
            ELSE 1
        END AS highest_votes_id
    FROM (
        SELECT
            M.id,
            M.name AS title_movie,
            M.year AS premiere,
            G.name AS gender,
            COUNT(MHR.vote_id) AS total_voting,
            COUNT(MHR.vote_id = 1 OR NULL) AS count_very_good,
            COUNT(MHR.vote_id = 2 OR NULL) AS count_good,
            COUNT(MHR.vote_id = 3 OR NULL) AS count_regular
        FROM movies M
        LEFT JOIN gender G ON M.gender_id = G.id
        LEFT JOIN movies_has_rating MHR ON M.id = MHR.movie_id
        GROUP BY M.id
    ) t
) t
ORDER BY t.id DESC

http://sqlfiddle.com/#!9/5ba403/7

如果你在SQL中解决这个问题,你最终会得到不合理的丑陋代码。你最好用PHP做。

if ($row['count_regular'] > $row['count_good'] &&
    $row['count_regular'] > $row['count_very_good']
) {
    $highestVoteType  = 'Regular';
    $highestVoteCount = $row['count_regular'];
} elseif ($row['count_good'] > $row['count_very_good']) {
    $highestVoteType  = 'Good';
    $highestVoteCount = $row['count_good'];
} else {
    $highestVoteType  = 'Very Good';
    $highestVoteCount = $row['count_very_good'];
}
if ($row['total_voting'] != 0) {
    $highestVotePercantage = 100 * $highestVoteCount / $row['total_voting'];
} else {
    $highestVotePercantage = null;
}

答案 1 :(得分:0)

1)

选择movies_has_rating.movi​​e_id,movies.name,count(movies_has_rating.vote_id)FROM movies_has_rating加入电影ON movies_has_rating.movi​​ed_id = movies.id