Skip to content

Instantly share code, notes, and snippets.

@mattm
Created November 1, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattm/4be8b679cc1ff999348e1cd417085ed2 to your computer and use it in GitHub Desktop.
Save mattm/4be8b679cc1ff999348e1cd417085ed2 to your computer and use it in GitHub Desktop.
SELECT users.user_id, SUM(IF(post_id IS NULL, 0, 1)) AS post_count
FROM users
LEFT JOIN posts ON posts.user_id = users.user_id
GROUP BY 1
+---------+------------+
| user_id | post_count |
+---------+------------+
| 1 | 3 |
| 2 | 2 |
| 3 | 0 |
+---------+------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment