Created
November 1, 2017 18:20
-
-
Save mattm/2ac3aafff39e3824f99a44517b6bcb6b 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 * | |
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 | |
+---------+-------+---------+------------+ | |
| user_id | name | user_id | post_count | | |
+---------+-------+---------+------------+ | |
| 1 | Matt | 1 | 3 | | |
| 2 | Simon | 2 | 2 | | |
| 3 | Jen | NULL | NULL | | |
+---------+-------+---------+------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment