[英]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.