Counting records across tables

Description SQL

The example in this query counts the number of events associated with each calendar.

It returns the calendar table's rows, along with a count of event table's rows associated with it.

The Code Download

  1. SELECT
  2.     c.*,
  3.     COUNT(e.id) AS cnt
  4. FROM
  5.     calendar c,
  6.     events e
  7. WHERE
  8.     e.calendar_id=c.calendar_id
  9. GROUP BY
  10.     c.calendar_id

Credits

Added by JC on 14th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment