此刻我有这个查询:
select p.idpost, count(c.idpost)
from posts p join comments c on p.idpost = c.idpost
group by p.idpost
结果仅显示带有评论的帖子。我怎样才能获得所有帖子?
此刻我有这个查询:
select p.idpost, count(c.idpost)
from posts p join comments c on p.idpost = c.idpost
group by p.idpost
结果仅显示带有评论的帖子。我怎样才能获得所有帖子?
使用 aleft join
而不是 ajoin
select p.idpost, count(c.idpost)
from posts p
left join comments c on p.idpost = c.idpost
group by p.idpost
这是使用代码的 SQL 连接的一个很好的可视化解释它显示了每个连接会发生什么
使用左连接:
select
p.idpost,
count(c.idpost)
from posts p
LEFT join comments c on p.idpost = c.idpost
group by p.idpost
干杯