Created
November 1, 2017 18:16
-
-
Save mattm/a6ffd4ac2645947c71bca9e2be97e927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
users.user_id, | |
COALESCE(post_count, 0) AS post_count, | |
COALESCE(page_count, 0) AS page_count | |
FROM users | |
LEFT JOIN ( | |
SELECT user_id, COUNT(*) AS post_count | |
FROM posts | |
GROUP BY user_id | |
) post_counts ON post_counts.user_id = users.user_id | |
LEFT JOIN ( | |
SELECT user_id, COUNT(*) AS page_count | |
FROM pages | |
GROUP BY user_id | |
) page_counts ON page_counts.user_id = users.user_id | |
+---------+------------+------------+ | |
| user_id | post_count | page_count | | |
+---------+------------+------------+ | |
| 1 | 3 | 0 | | |
| 2 | 2 | 2 | | |
| 3 | 0 | 5 | | |
+---------+------------+------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment