Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如果百分比高于某个值,则连接表_Mysql_Sql - Fatal编程技术网

Mysql 如果百分比高于某个值,则连接表

Mysql 如果百分比高于某个值,则连接表,mysql,sql,Mysql,Sql,我的问题与此类似: 然而,没有什么不同。我把我的问题改编成另一篇文章 我有两张桌子 第一张表: user_id | post_id 1 1 1 2 1 3 2 12 2 15 第二表: post_id | rating 1 1 1 2 1 3 2 1 2 5 3 1 3 1 3 4 1

我的问题与此类似:

然而,没有什么不同。我把我的问题改编成另一篇文章

我有两张桌子

第一张表:

user_id | post_id
1         1
1         2
1         3
2         12
2         15
第二表:

post_id | rating
1         1
1         2
1         3 
2         1
2         5
3         1
3         1
3         4
12        4
15        1
所以现在我想在第二张表中计算每个帖子的评分。 如果评分超过,比方说,50%的积极评分超过我想要的post_id,并将其从表1中转到post_id,并将1添加到用户_id

最后,它将返回带有肯定帖子数量的用户id

上表的结果为:

user_id | helpfulPosts
1         2
2         1
具有post_id 1和post_id 3的帖子的评级为正,因为超过50%的帖子的评级为1-3。id=2的帖子不是正面的,因为评级正好是50%

我将如何实现这一点

请澄清: 这是一篇mysql rdbm和一篇正面文章,其中1、2和3的评级ID数量超过了整体评级的一半。基本上是一样的,从我上面发布的另一个帖子

忘了一件事: posts表中也可能存在一个post_id,但在ratings_表中没有对其进行评级。这些帖子也很有用。 以
null
作为评级的案例在我这方面是一个误解。

尝试以下解决方案:

SELECT
    a.user_id,
    COUNT(1) AS helpfulPosts
FROM
    posts a
LEFT JOIN
    (
        SELECT 
            post_id, 
            COUNT(CASE WHEN rating IN (1,2,3) OR rating IS NULL THEN 1 END) / COUNT(1) AS percent_positive
        FROM ratings
        GROUP BY post_id
    ) b ON a.post_id = b.post_id
WHERE
    b.post_id IS NULL OR
    b.percent_positive > 0.5
GROUP BY
    a.user_id


^请注意,我在用户id
1
中添加了没有评分的帖子,这些帖子被计入用户的
帮助帖子

要解决这个问题,您需要首先确定哪些帖子是有帮助的。使用您的逻辑,这只是计算平均评分,当评分存在时

select u.user_id, count(*) as HelpfulPosts
from UserPosts u join
     (select post_id,
             sum(case when rating in (1, 2, 3) then 1.0 else 0.0 end) / count(rating) as HelpfulRating 
      from PostRating pr
      group by post_id
     ) r
     on r.post_id = u.post_id
where r.HelpfulRating > 0.5
group by user_id
下一步是将其连接回user posts表,按用户id分组,以统计有用帖子的数量

顺便说一下,我不认为“3”有什么帮助。你是说15岁?上面的查询忽略空评级。如果认为NULL有帮助,则使用:

             sum(case when coalesce(rating, 1) in (1, 2, 3) then 1.0 else 0.0 end) / count(*) as HelpfulRating 

而不是查询中的版本。

您可能应该解释“积极评级”的含义。我想我现在明白了,但当我第一次读到你的问题时,我并不清楚。只是好奇,什么类型的SQL DBMS?我修改了我的解决方案,以说明没有任何评级的帖子,并将这些帖子作为“有帮助的帖子”以及那些正面评级超过50%的帖子。是的,帖子3是有帮助的,因为它没有评级(null),一个等级为1,一个等级为4。因此,总体评级为66667%(空值被视为1、2和3)。您需要将
count(rating)
更改为
count(*)
,否则它不会将
null
评级计入评级总数。很抱歉,这实际上是我的错。空值不应计入评级本身。没有空值作为评级。请看我第一篇文章的最后一段。这避免了整数除法的整个(次要)问题。我遇到过很多人忘记了这一点。我之所以修改,是因为你提到空值对积极评价的贡献。如果你发现一种方式比另一种方式读得更清楚,那么你有多种方式来表达积极的评级条件。
select up.user_id, count(up.post_id) as helpfulPosts
from userposts as up
where up.post_id in (
    select pr.post_id
    from postratings as pr
    group by pr.post_id
    having
        sum(case when pr.rating between 4 and 5 then 0 else 1 end) > 
        sum(case when pr.rating between 4 and 5 then 1 else 0 end)
)
group by up.user_id