如果我有两张桌子
和
我想要一个看起来像这样的结果:
SELECT app_status.title, count(*) as count FROM application as app LEFT JOIN application_status as app_status ON app.application_status_id = app_status.application_status_id GROUP BY app.application_status_id;
我已尝试过上述查询,但只返回值(如果存在)。这意味着它将计算> 0但是如果它= 0那么它不会包含那一行并最终看起来像这样
答案 0 :(得分:2)
您的left join
顺序错误。另外:
count()
更改为第二个表中的计数。group by
以匹配第一个select
列。left join
。因此,生成的查询是:
SELECT app_status.title, count(app.application_status_id) as count
FROM application_status app_status LEFT JOIN
application app
ON app.application_status_id = app_status.application_status_id
GROUP BY app_status.title
ORDER BY MIN(app_status.application_status_id);