Mysql sql分组依据,尽管没有值
我有一个表,其中有4个这样的条目Mysql sql分组依据,尽管没有值,mysql,count,group-by,Mysql,Count,Group By,我有一个表,其中有4个这样的条目 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
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(*)和group by id_class_type,但是要使用所有class_type(1,2,3,4),尽管不存在id 4 如果只需要匹配的类,可以使用内部联接
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
将例外结果也作为文本表包含。阅读关于外部联接的内容。