I have two tables
documents: id, folder_id
folders: id, title
I want to retrieve a list of folders and count of how many files relate to that folder (documents.folder_id = folders.id)
I tried this
select
f.title, count(d.id) as file_count
from
folders f
left join
documents d on f.id = d.folder_id
But that only returns one row, with the total number of rows in the documents table
You’re missing a
GROUP BYclause to return a row perf.title.Note that the above will omit empty folders, I believe. If you want a zero count for the empty folders, use
COUNT(d.*)instead: