The following query should get you what you want:
SELECT c.cat_id, COUNT(a.album_id)
FROM ci_categories AS c
LEFT JOIN ci_albums AS a ON c.cat_id = a.cat_id
GROUP BY c.cat_id
We use a LEFT JOIN
here due to the possibility of categories which contain no albums. If you don't care if those results show up, just change this to an INNER JOIN
.
We use the aggregate COUNT(a.album_id)
function to count the number of records. By grouping by the c.cat_id
, we make sure this count is done only over records of the same type. If there is no album for that category, a.album_id will be NULL, and so COUNT
will not include it in the total count.