Skip to content

Instantly share code, notes, and snippets.

@ElMatella
Last active December 19, 2017 17:33
Show Gist options
  • Save ElMatella/8aaef58cc6ed6bd582d568c36b8db82f to your computer and use it in GitHub Desktop.
Save ElMatella/8aaef58cc6ed6bd582d568c36b8db82f to your computer and use it in GitHub Desktop.
Table users:
+----+---------+
| id | name |
+----+---------+
| 1 | Mathieu |
| 2 | Ynk |
+----+---------+
Table articles:
+----+-----------+-------------------+
| id | user_id | title |
+----+-----------+-------------------+
| 1 | 1 | Mon super article |
+----+-----------+-------------------+
SELECT * FROM users INNER JOIN articles ON articles.user_id = users.id
+---------+-----------+------------+-------------------+-------------------+
| user.id | user.name | article.id | article.user_id | article.title |
+---------+-----------+------------+-------------------+-------------------+
| 1 | Mathieu | 1 | 1 | Mon super article |
+---------+-----------+------------+-------------------+-------------------+
SELECT * FROM users LEFT JOIN articles ON articles.user_id = users.id
+---------+-----------+------------+-------------------+-------------------+
| user.id | user.name | article.id | article.user_id | article.title |
+---------+-----------+------------+-------------------+-------------------+
| 1 | Mathieu | 1 | 1 | Mon super article |
| 2 | Ynk | null | null | null |
+---------+-----------+------------+-------------------+-------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment