You have two tables galleries and images, and you need to know how many images each gallery has associated with it. This Query returns the sum of distinct record joined to another table by a common field (gallery_id).
- SELECT DISTINCT g.gallery_name, g.gallery_id, count(g.gallery_id) AS sum
- FROM galleries AS g
- JOIN images AS i
- ON g.gallery_id = i.gallery_id
- GROUP BY g.gallery_id
- ORDER BY sum ASC
You need to login to post a comment.