2017-06-27 102 views
-1

我有一個表存在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

回答

0

類塔比賽,你可以使用內部連接

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