Données · SQL
Comment fonctionne une jointure SQL ?
3 clients, une commande chacun : combien de lignes dans la jointure ?
Et si l'un des trois clients a deux commandes ?
D'une commande par client à une jointure qui double des lignes, cinq points de passage :
Comment une jointure SQL relie-t-elle deux tables ?
La ligne ON dit quelles colonnes comparer. Ici, la commande dont le client_id vaut 7 se range à côté du client dont l'id vaut 7.
Prenons trois clients et leurs commandes.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 2 | 25 € |
| 103 | 3 | 60 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Malik | 25 € |
| Inès | 60 € |
Quelle commande porte le numéro de Léa ? On la touche dans la table.
Léa a l'
id1. On cherche ce 1 dans la colonneclient_id.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 40 € 102 2 25 € 103 3 60 € 104 4 15 € SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade commandes id client_id montant 104 4 15 € 102 2 25 € 101 1 40 € 103 3 60 € 105 5 80 €
Pourquoi un JOIN fait-il disparaître des lignes ?
Avec JOIN, aussi écrit INNER JOIN, le client dont l'id vaut 7 ne sort pas si aucune commande ne porte le client_id 7.
Prenons trois clients, dont un sans commande.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 3 | 25 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Inès | 25 € |
Quelle commande porte le numéro de Malik ?
Malik a l'
id2. On cherche ce 2 dans la colonneclient_id.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 40 € 102 2 25 € 103 4 60 € SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade commandes id client_id montant 102 3 25 € 101 2 40 € 103 5 60 €
Quelle différence entre JOIN et LEFT JOIN ?
LEFT JOIN garde chaque ligne de la table de gauche, celle écrite avant lui. Le client dont l'id vaut 7 sort, qu'une commande porte le client_id 7 ou non.
Prenons les mêmes tables, avec LEFT JOIN.
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 3 | 25 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Malik | NULL |
| Inès | 25 € |
Que contient la colonne
montantsur la ligne de Malik ?
SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 40 € 102 2 25 € 103 4 60 € SELECT nom, montant FROM commandes LEFT JOIN clients ON commandes.client_id = clients.id
commandes id client_id montant 104 7 15 € 102 4 25 € 101 2 40 € 103 5 60 € clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo 5 Jade
Pourquoi une jointure SQL crée-t-elle des doublons ?
Un client qui a 5 commandes sort 5 fois : la jointure écrit une ligne par paire client-commande, pas une ligne par client.
Prenons trois clients, dont un avec deux commandes.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
| id | nom |
|---|---|
| 1 | Léa |
| 2 | Malik |
| 3 | Inès |
| id | client_id | montant |
|---|---|---|
| 101 | 1 | 40 € |
| 102 | 1 | 25 € |
| 103 | 2 | 60 € |
| 104 | 3 | 15 € |
| nom | montant |
|---|---|
| Léa | 40 € |
| Léa | 25 € |
| Malik | 60 € |
| Inès | 15 € |
Quelles commandes portent le numéro de Léa ?
Léa a l'
id1. Ce 1 peut apparaître plusieurs fois dans la colonneclient_id.Si l'on compte les lignes de ce résultat pour compter les clients, lequel est compté deux fois ?
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès commandes id client_id montant 101 1 40 € 102 2 25 € 103 2 60 € 104 2 15 € 105 3 80 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 104 3 15 € 102 1 25 € 106 4 35 € 101 1 40 € 103 3 60 € 105 3 80 €
Comment lire vite une jointure SQL ?
On va plus vite en regardant d'abord quelle table est écrite avant JOIN : on prend ses lignes une par une, et on cherche pour chacune celles de l'autre table qui portent le même numéro.
SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 101 1 40 € 102 3 25 € 103 4 60 € SELECT nom, montant FROM clients JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès commandes id client_id montant 104 3 15 € 102 1 25 € 101 1 40 € 103 2 60 € 105 3 80 € SELECT nom, montant FROM clients LEFT JOIN commandes ON commandes.client_id = clients.id
clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo commandes id client_id montant 102 4 25 € 101 1 40 € SELECT nom, montant FROM commandes LEFT JOIN clients ON commandes.client_id = clients.id
commandes id client_id montant 104 3 15 € 102 1 25 € 106 4 35 € 101 1 40 € 107 6 50 € 103 1 60 € 105 4 80 € clients id nom 1 Léa 2 Malik 3 Inès 4 Hugo