我有2个表(通道和问题,其中问题有通道的外键)
我的查询如下:
SELECT p."id", p."passageTitle", COUNT(q."PassageId") as numQuestions
FROM "Passages" as p
LEFT JOIN "Questions" as q
ON p.id=q."PassageId"
WHERE q."status" = 'active'
GROUP BY p.id
ORDER BY p.id ASC
我的目标是在另一列上获取“状态=有效的问题”的计数,但如果该“段落”没有“状态=有效的问题”为0。
我的问题是这个查询只返回有q的行。“status”='active'但我仍然希望包含不符合此条件的行。我将如何添加到我的查询中以使其成为可能?
答案 0 :(得分:2)
仅与Questions
status = active
SELECT p."id", p."passageTitle", COUNT(q."PassageId") as numQuestions
FROM "Passages" as p
LEFT JOIN "Questions" as q
ON p.id = q."PassageId" AND q."status" = 'active'
GROUP BY p.id
ORDER BY p.id ASC