Skip to content

Instantly share code, notes, and snippets.

@jcamachott
Created May 16, 2013 01:33
Show Gist options
  • Save jcamachott/5588772 to your computer and use it in GitHub Desktop.
Save jcamachott/5588772 to your computer and use it in GitHub Desktop.
Sub query counts
// table called categories, with an id and a parent_category_id
// useful for counting number of subcategories per parent category
SELECT *,
(SELECT count(*) FROM categories c1 where c1.id=categories.parent_category_id)
as TheCount FROM `categories`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment