Posted By

jbranca on 09/10/10


Tagged

mysql count join


Versions (?)

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


 / Published in: MySQL
 

URL: count-of-joined-records

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

  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
  6. ORDER BY sum ASC

Report this snippet  

You need to login to post a comment.