Last active
November 2, 2017 14:49
-
-
Save mattm/c2ce581d5357187e0939816eae7e1888 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, | |
SUM(IF(post_id IS NULL, 0, 1)) AS post_count, | |
SUM(IF(page_id IS NULL, 0, 1)) AS page_count | |
FROM users | |
LEFT JOIN posts ON posts.user_id = users.user_id | |
LEFT JOIN pages ON pages.user_id = users.user_id | |
GROUP BY 1 | |
# or | |
SELECT | |
users.user_id, | |
COUNT(post_id) AS post_count, | |
COUNT(page_id) AS page_count | |
FROM users | |
LEFT JOIN posts ON posts.user_id = users.user_id | |
LEFT JOIN pages ON pages.user_id = users.user_id | |
GROUP BY 1 | |
+---------+------------+------------+ | |
| user_id | post_count | page_count | | |
+---------+------------+------------+ | |
| 1 | 3 | 0 | | |
| 2 | 4 | 4 | | |
| 3 | 0 | 5 | | |
+---------+------------+------------+ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment