【发布时间】: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。