0
在這一刻我有這個疑問:SQL:獲取與任何評論所有文章(包括0)
select p.idpost, count(c.idpost)
from posts p join comments c on p.idpost = c.idpost
group by p.idpost
結果只顯示與評論的帖子。我如何獲得全部的帖子?
謝謝
在這一刻我有這個疑問:SQL:獲取與任何評論所有文章(包括0)
select p.idpost, count(c.idpost)
from posts p join comments c on p.idpost = c.idpost
group by p.idpost
結果只顯示與評論的帖子。我如何獲得全部的帖子?
謝謝
使用LEFT JOIN:
select
p.idpost,
count(c.idpost)
from posts p
LEFT join comments c on p.idpost = c.idpost
group by p.idpost
乾杯
使用left join
代替join
select p.idpost, count(c.idpost)
from posts p
left join comments c on p.idpost = c.idpost
group by p.idpost
Here is a good visual explanation of SQL joins with code它表明會發生什麼,每個加入