これはgallery でグループ化された初歩的な集合体COUNT()
name
です。
重要なことに、これは a を使用して、LEFT JOIN
画像を持たないギャラリーが行をまったく返さないのではなくゼロ カウントを返すようにする必要があります。
SELECT
galleries.name,
COUNT(images.img_id) AS total_images
FROM
galleries
/* LEFT JOIN will ensure empty galleries are listed */
LEFT JOIN images ON galleries.gal_id = images.gal_id
GROUP BY galleries.name
/* Sort by descending total_images */
ORDER BY total_images DESC
コメント後に更新
SELECT
galleries.name,
COUNT(images.img_id) AS total_images,
/* Differentiate between downloadable/not downloadable with a SUM(CASE) */
/* It converts each into a zero or one and then sums up the values */
SUM(CASE WHEN downloadable = 'Y' THEN 1 ELSE 0 END) AS downloadable,
SUM(CASE WHEN downloadable = 'N' THEN 1 ELSE 0 END) AS not_downloadable
FROM
galleries
/* LEFT JOIN will ensure empty galleries are listed */
LEFT JOIN images ON galleries.gal_id = images.gal_id
GROUP BY galleries.name
/* Sort by descending total_images */
ORDER BY total_images DESC