选择COUNT个内联接

时间:2018-09-27 13:44:06

标签: php mysql

我有两个表:

project (id, name)
list (id, name, project_id)

我想将每个项目和列表号都放入该项目。

我尝试过:

SELECT project.id, project.name, COUNT(list.id) AS list_count
FROM project 
INNER JOIN list ON (list.project_id = project.id)
GROUP BY project.id DESC

但是我只得到有列表的项目。

如果该项目没有列表,我不会得到...

如果COUNT(list_id)= 0,我希望list_count = 0,我该怎么办?

1 个答案:

答案 0 :(得分:5)

使用左联接代替内联接

SELECT project.id, project.name, COUNT(list.id) AS list_count
FROM project 
Left JOIN list ON (list.project_id = project.id)
GROUP BY project.id DESC