Skip to content

Instantly share code, notes, and snippets.

@julp
Last active July 28, 2023 09:22
Show Gist options
  • Save julp/e3ef79aaec837252fb4c2dbb0431adfb to your computer and use it in GitHub Desktop.
Save julp/e3ef79aaec837252fb4c2dbb0431adfb to your computer and use it in GitHub Desktop.
[OC] Requête SQL complexe (4 jointures LEFT sur la même table)
<?php
$stmt = $bdd->query(<<<'EOS'
SELECT
ds.sejour_nom,
ds.id,
ii1.nom,
ii2.nom,
ii3.nom,
ii4.nom
FROM tournee_sejour ts
LEFT JOIN doc_sejour ds ON ts.id_sejour = ds.id
LEFT JOIN index_institutions ii1 ON ts.arret_1 = ii1.id_institution
LEFT JOIN index_institutions ii2 ON ts.arret_2 = ii2.id_institution
LEFT JOIN index_institutions ii3 ON ts.arret_3 = ii3.id_institution
LEFT JOIN index_institutions ii4 ON ts.arret_4 = ii4.id_institution
WHERE ds.sejour_actif = 1
EOS
);
?>
<table>
<?php while ($donnees = $reponse->fetch(PDO::FETCH_NAMED)): ?>
<tr>
<td>
<?= htmlspecialchars($donnees['sejour_nom']) ?>
</td>
<?php for ($i = 0; $i < 4; $i++): ?>
<td>
<?= htmlspecialchars(null === $donnees['nom'][$i] ? '-' : $donnees['nom'][$i]) ?>
</td>
<?php endfor ?>
</tr>
<?php endwhile ?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment