使用零计数对左连接和内连接分组结果

时间:2015-01-27 17:13:34

标签: mysql sql join count group-by

我有一个像

这样的查询
SELECT 
    `campaign_question_options`.`text`, 
    COUNT(`campaign_submission_answers`.`answer`) as `count`
    FROM `campaign_questions`
    INNER JOIN `campaign_question_options` ON `campaign_question_options`.`campaign_question_id` = `campaign_questions`.`id`
    LEFT JOIN `campaign_submission_answers` ON `campaign_submission_answers`.`answer` = `campaign_question_options`.`text` AND `campaign_submission_answers`.`campaign_question_id` = 1
    LEFT JOIN `campaign_submissions` ON `campaign_submissions`.`id` = `campaign_submission_answers`.`campaign_submission_id`
    LEFT JOIN `participants` ON `participants`.`id` = `campaign_submissions`.`participant_id`
WHERE 
    `campaign_questions`.`id` = 1
GROUP BY `campaign_submission_answers`.`answer` 
ORDER BY `campaign_question_options`.`index`;

这给了我一个像

这样的结果集
+--------------+-------+
|     text     | count |
+--------------+-------+
| 1 (positive) |   114 |
| 2            |    48 |
| 3 (neutral)  |    34 |
| 4            |     6 |
| 5 (negative) |     0 |
+--------------+-------+

所以问题是我需要在participantsappraisee_id列上进一步过滤结果。但是,如果我将其添加到where子句,则会丢失零结果(因为左连接返回空行)。

SELECT 
    `campaign_question_options`.`text`, 
    COUNT(`campaign_submission_answers`.`answer`) as `count`
FROM `campaign_questions`
INNER JOIN `campaign_question_options` ON `campaign_question_options`.`campaign_question_id` = `campaign_questions`.`id`
LEFT JOIN `campaign_submission_answers` ON `campaign_submission_answers`.`answer` = `campaign_question_options`.`text` AND `campaign_submission_answers`.`campaign_question_id` = 1
LEFT JOIN `campaign_submissions` ON `campaign_submissions`.`id` = `campaign_submission_answers`.`campaign_submission_id`
LEFT JOIN `participants` ON `participants`.`id` = `campaign_submissions`.`participant_id`
WHERE 
    `campaign_questions`.`id` = 1 AND `participants`.`appraisee_id` = 1
GROUP BY `campaign_submission_answers`.`answer` 
ORDER BY `campaign_question_options`.`index`;

返回

+--------------+-------+
|     text     | count |
+--------------+-------+
| 1 (positive) |    16 |
| 2            |     1 |
+--------------+-------+

实际上我希望

+--------------+-------+
|     text     | count |
+--------------+-------+
| 1 (positive) |    16 |
| 2            |     1 |
| 3 (neutral)  |     0 |
| 4            |     0 |
| 5 (negative) |     0 |
+--------------+-------+

有人可以帮我改进这个查询吗?

由于

更新

我已经创建了一个结构的数据库转储,如果有任何人希望继续帮助我,这可能很有用。 https://gist.github.com/simonbowen/a8316fe91c78b8464402

3 个答案:

答案 0 :(得分:1)

当你有left join并希望过滤除第一个表之外的任何表时,你需要将条件放在on子句中:

SELECT cqo.`text`, 
       COUNT(csa.`answer`) as `count`
FROM `campaign_questions` cq INNER JOIN
     `campaign_question_options` cqo 
     ON cqo.`campaign_question_id` = cq.`id` LEFT JOIN
     `campaign_submission_answers` csa
     ON csa.`answer` = cqo.`text` AND csa.`campaign_question_id` = 1 LEFT JOIN
     `campaign_submissions` cs
     ON cs.`id` = csa.`campaign_submission_id LEFT JOIN
     `participants` p
     ON p.`id` = cs.`participant_id` AND
        p. appraisee_id = XXX
WHERE cq.`id` = 1
GROUP BY csa.`answer` 
ORDER BY cqo.`index`;

我还添加了表别名。它们使查询更容易编写和阅读。

答案 1 :(得分:0)

添加另一张支票participantsappraisee_idNULL

WHERE `campaign_questions`.`id` = 1 
  AND (`participants`.`appraisee_id` = 1 
   OR `participants`.`appraisee_id` IS NULL)

答案 2 :(得分:0)

关于这个问题的更新,我尝试从不同的角度尝试此查询。它似乎输出了我期望的结果,但是我不确定它是否是最有效的方式,因为我必须使用子查询。

SELECT `campaign_question_options`.`text`, COUNT(`csa`.`answer`) FROM `campaign_questions`
INNER JOIN `campaign_question_options`  ON `campaign_question_options`.`campaign_question_id` = `campaign_questions`.`id`
LEFT JOIN (
   SELECT `campaign_submission_answers`.* FROM `campaign_submission_answers`
   INNER JOIN `campaign_submissions` ON `campaign_submissions`.`id` = `campaign_submission_answers`.`campaign_submission_id`
   INNER JOIN `participants` ON `participants`.`id` = `campaign_submissions`.`participant_id`
   INNER JOIN `campaign_questions` ON `campaign_questions`.`id` = `campaign_submission_answers`.`campaign_question_id`
   INNER JOIN `campaign_question_options` ON `campaign_question_options`.`text` = `campaign_submission_answers`.`answer` 
   WHERE `campaign_submissions`.`campaign_id` = 1 AND `participants`.`appraisee_id` = 1 AND `campaign_submission_answers`.`campaign_question_id` = 1
   GROUP BY `campaign_submission_answers`.`id`
) as `csa` ON `csa`.`answer` = `campaign_question_options`.`text`
WHERE `campaign_questions`.`id` = 1
GROUP BY `campaign_question_options`.`text`;