Number of records in one table of a type from another table.


/ Published in: MySQL
Save to your folder(s)

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).


Copy this code and paste it in your HTML
  1. SELECT DISTINCT g.gallery_name, g.gallery_id, count(g.gallery_id) AS sum
  2. FROM galleries AS g
  3. JOIN images AS i
  4. ON g.gallery_id = i.gallery_id
  5. GROUP BY g.gallery_id

URL: count-of-joined-records

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.