我的数据库中有两个表:
- 应用程序(id、类型、user_id、status_id)。
- 状态(id,名称);
我想按类型分组以获取每种类型的计数,然后作为第三列,我想获取该计数中有多少 status_id 为 3
像这样的东西:
类型 | type_count | status_3_count |
---|---|---|
类型1 | 4 | 2 |
类型2 | 10 | 5 |
我试过这个,但它不起作用。
select a.type, count(*) type_count, count(b.id) status_3_count
from applications a
join statuses b on b.id = a.status_id and b.id = 3
group by a.type, b.id