sql组虽然没有值

问题描述:

我有一个表存在4个这样的条目。sql组虽然没有值

class_type 

id type 
1  A 
2  B 
3  M 
4  T 

和另一个表中这些值是外键。

id number id_class_type 
1  10   1 
2  11   1 
3  12   2  
4  13   1 
5  14   2 
6  15   3 
7  16   1 
8  17   3 

所以我想是COUNT(*)和组由id_class_type但所有class_type(1,2,3,4)虽然有,如果你只想要不存在的ID 4.

+0

包含的除外结果也为文本表.. –

+0

阅读关于外部连接。 – mustaccio

类塔比赛,你可以使用内部连接

select a.class_type, count(*) 
    from class_type a 
    inner join table2 b on a.id = b.id_class_type 
    group by a.class_type 

否则,你可以使用左加入

select a.class_type, count(*) 
    from class_type a 
    left join table2 b on a.id = b.id_class_type 
    group by a.class_type