【发布时间】:2020-12-15 09:20:00
【问题描述】:
我有2张桌子如下
人类
id name
1 John
2 mark
电脑
id human_id
1 2
2 null
我希望我的回答如下
name no_of_computer
John 0
mark 1
我尝试了以下但错了
select h.name,c.human_id) as 'no_of_computer' from human h
join
computer c on c.human_id = h.id
group by h.id
我的代码没有将 null 计为 0,因此它只是避免了这种情况,只显示该标记有 1 台计算机,但我想看到 John 也有 0 台计算机。请帮帮我,在此先感谢
【问题讨论】:
-
试试
LEFT加入 -
您发布的代码在语法上不正确,请更正。以及 p.id 来自哪里?
标签: mysql database join count null