[英]select from one table, count from another where id is not linked
你需要一个左连接。 这种类型的联接显示左表中的所有行,即使右表中不存在任何行。
select t2.name, count(t1.id)
from table2 as t2
left join table1 as t1 on t2.name = t1.status
group by t2.name
请注意,您需要聚合table1
的列以生成所需的0
,因此是count(t1.id)
。 即使table1
没有行, count(*)
也会产生1
。
查询中的交叉联接只是创建所涉及的两个表的笛卡尔积,导致左表中的每一行与右表中的每一行连接一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.