【问题标题】:How can I count the number of posts that have either zero or positive vote score?如何计算投票得分为零或正面的帖子数量?
【发布时间】:2016-09-09 01:28:13
【问题描述】:

我有这张桌子:

// qanda
+----+----------------------------------------+---------+-----------+------+
| Id |                   body                 | related |  user_id  | free |
+----+----------------------------------------+---------+-----------+------+
| 1  | content of question1                   | null    | 2         | null |
| 2  | content of first answer for question1  | 1       | 2         | null |
| 3  | content of question2                   | null    | 6         | 300  |
| 4  | content of second answer for question1 | 1       | 4         | null |
| 5  | content of first answer for question2  | 3       | 2         | null |
| 6  | content of question3                   | NULL    | 8         | null |
| 7  | content of first answer for question3  | 6       | 4         | null |
| 8  | content of second answer for question3 | 6       | 2         | null |
+----+----------------------------------------+---------+-----------+------+

我有这个查询:

SELECT count(1)
 FROM qanda question
 JOIN qanda answer ON question.Id = answer.related
WHERE answer.related IS NOT NULL
  AND answer.user_id = 2
  AND question.free IS NULL;

如您所知,上面的查询返回属于免费问题的答案数量(在本例中为it returns 2。好的,一切都好。我也有这张桌子:

// votes
+----+---------+-------+
| id | post_id | value |
+----+---------+-------+
| 1  | 2       |  1    |
| 2  | 3       | -1    |
| 3  | 2       |  1    |
| 4  | 8       | -1    |
| 5  | 1       |  1    |
| 6  | 4       |  1    |
| 7  | 2       | -1    |
| 8  | 8       |  1    |
| 9  | 8       | -1    |
| 10 | 8       | -1    |
+----+---------+-------+

我需要消除总分为负的答案。所以在这种情况下它应该返回1。因为qanda.id = 8,那个答案有-2的总票数,所以我不想计算它。我该怎么做?

【问题讨论】:

  • 能否请您发布您想要的输出。
  • @Suraz 预期输出只是1

标签: mysql sql count


【解决方案1】:

排除总票数小于零的帖子的最简单方法如下:

SELECT count(1)
FROM qanda question
JOIN qanda answer ON question.Id = answer.related
WHERE answer.related IS NOT NULL
AND answer.user_id = 2
AND question.free IS NULL
AND question.id not in (
  select post_id
  from votes
  group by post_id
  having sum(value) < 0)

这里的关键部分是having sum(value) &lt; 0,它选择了净反对票的帖子。


来自 cmets...

要查找有太多“坏”答案的用户,您可能应该返回他们做出的“好”答案的数量,并确定他们总体上是否是“坏”用户。例如,一个用户有 5 个答案都不好,这与一个用户有 1000 个答案,其中只有 5 个不好,尽管他们都有 5 个不好的答案。

试试这个:

select
    sum(score < 0) bad,
    count(*) total,
    sum(score < 0) / sum(.01) percent_bad
from (
    SELECT coalesce(sum(value), 0) score
    FROM qanda question
    JOIN qanda answer ON question.Id = answer.related
    LEFT JOIN votes ON votes.post_id = answer.id
    WHERE answer.related IS NOT NULL
    AND answer.user_id = 2
    AND question.free IS NULL
    AND answer.timestamp > subdate(now(), 365)
    GROUP BY answer.id
) scores

里面有一些关于 SQL Kung Fu 的注释:

  • 在 MySQL 中,true 为 1,false 为 0,因此通过对条件求和,您可以计算它为 true 的次数。这比其他 DB 所需的笨拙的 SUM(CASE ...) 表达式更易于编码和阅读
  • 通过SUM(.01) 计算计数(我只是想到了顺便说一句)是获得百分比的最简单方法,因为它不仅简化了表达式,而且协调了浮点数的答案,因此您可以自动避免整数算术舍入

免责声明:代码可能无法编译或工作,因为它是在我的手机上翻阅的(但它很有可能会工作)

【讨论】:

  • 为什么是question.id not in (?我的意思是为什么不answer.id not in (
  • @stack 抱歉 - 其他方式:向内部查询添加 where 子句会减少行数,但只会删除无关紧要的行
  • @stack 和 OP:我已经添加了时间框架约束
  • @shaf 是的,是的
  • @MartinAJ 使用左连接,您必须将条件置于连接条件内,否则您将有效地强制连接成为正常的内连接。您的代码应如下所示:LEFT JOIN votes ON votes.post_id = answer.id and votes.table_code = 15
【解决方案2】:

您可以放置​​另一个 AND 条件,其中包含 select 语句(求和值,其中 post id = qanda.id )用于计算总投票分数并将其评估为大于或等于零。

【讨论】:

  • 我想我需要一个JOIN 子句到votes 表。
  • 是的,您也可以添加另一个联接。两者都达到相同的结果
【解决方案3】:

请尝试一次:

SELECT v.value val
FROM qanda q
INNER JOIN qanda q1 ON q1.related = q.id
INNER JOIN votes v ON v.id = q.user_id
WHERE q.free IS NULL 
AND q1.user_id = 2
AND v.value > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多