Requète SELECT avec jointure

Résolu
Bonjour,
J'ai une requète avec jointure avec laquelle tout se passe bien
là voici
 $sql = "SELECT *
   FROM tb_gestion_admin
   INNER JOIN tb_adherent
   ON tb_adherent.id_adherent = tb_gestion_admin.rid_adherent
   WHERE id_adherent = :id_adherent
   AND actif = 1";
 $requete = $bdd->prepare($sql);       
 $requete->execute(array(
      'id_adherent' => $id_adherent
      ));

mais il faut que je rajoute une table et là mon formulaire ne s'affiche plus.
Pouvez me dire, s'ils vous plait si ma requète avec 3 tables est bonne pour savoir si mon problème est lié avec cette requète.
Je vous remercie
 $sql = "SELECT *
   FROM tb_gestion_admin
   INNER JOIN (tb_adherent
   ON tb_adherent.id_adherent = tb_gestion_admin.rid_adherent)
   INNER JOIN tb_participant
   ON tb_adherent.id_adherent = tb_participant.rid_adherent
   WHERE id_adherent = :id_adherent
   AND actif = 1";
 $requete = $bdd->prepare($sql);       
 $requete->execute(array(
      'id_adherent' => $id_adherent
      ));

4 réponses

  1. Modérateur
    Bonjour,

    1 - As tu testé ta requête en direct dans ta BDD ??
    au cas où, regarde ici : https://codes-sources.commentcamarche.net/faq/10778-heidisql-tester-ses-requetes-sql

    2 - A quoi servent ces parenthèses dans tes jointures ?
    Essayes comme ceci :
    $sql = "SELECT *
            FROM tb_gestion_admin
            INNER JOIN tb_adherent  ON tb_adherent.id_adherent = tb_gestion_admin.rid_adherent
            INNER JOIN tb_participant   ON tb_adherent.id_adherent = tb_participant.rid_adherent
            WHERE tb_gestion_admin.id_adherent = :id_adherent
               AND actif = 1";
    
     $requete = $bdd->prepare($sql);       
     $requete->execute(array('id_adherent' => $id_adherent ));
    
    


    et en utilisant des ALIAS pour le nom de tes Tables...ça devient encore plus lisible
    $sql = "SELECT *
            FROM tb_gestion_admin G
            INNER JOIN tb_adherent A   ON A.id_adherent = G.rid_adherent
            INNER JOIN tb_participant P ON A.id_adherent = P.rid_adherent
            WHERE G.id_adherent = :id_adherent
              AND actif = 1";
    
     $requete = $bdd->prepare($sql);       
     $requete->execute(array('id_adherent' => $id_adherent ));
    


    Cordialement,
    Jordane
    1. Bonjour
      En fait mes tables sont
      table tb_adherent
      id_adherent
      etc...(sans interet)
      
      table tb_gestion_admin
      id_gestion_admin
      rid_adherent
      etc...
      
      table tb_participant
      id_participant
      rid_adherent
      etc...
      

      et avec cette requète comme je le disais n'affiche pas mon formulaire et aucun message est retourné.
      	$sql = "SELECT *
      			FROM tb_gestion_admin
      			INNER JOIN tb_adherent
      			ON tb_adherent.id_adherent = tb_gestion_admin.rid_adherent
      			INNER JOIN tb_participant
      			ON tb_adherent.id_adherent = tb_participant.rid_adherent
      			WHERE id_adherent = :id_adherent
      			AND actif = 1";
      	$requete = $bdd->prepare($sql);							
      	$requete->execute(array(
      						'id_adherent' => $id_adherent
      						));
      

      En fait j'affiche le formulaire avec ce lien pour pouvoir modifier les infos des membres.
      Dans ma BDD j'ai testé cette requète et elle ne fonctionne pas pourtant elle ma semble correct.
      <a href="./index.php?body=modif_membre&type=admin&id_adherent=<?php echo $donnees['id_adherent'];?>"></a>
      

      Merci de ton coup de main
      1. Modérateur

        Dans ma BDD j'ai testé cette requète et elle ne fonctionne pas pourtant elle ma semble correct.

        Et bien si elle ne fonctionne pas dans ta BDD ... normal que ça ne t'affiche rien....
        c'est donc qu'elle n'est pas bonne....
        A la limite... essaye de faire des LEFT JOIN à la place des INNER
        et donc.. en direct dans ta BDD .. tu peux tester :
        SELECT *
        FROM tb_gestion_admin G
        LEFT JOIN tb_adherent A ON A.id_adherent = G.rid_adherent
        LEFT JOIN tb_participant P ON A.id_adherent = P.rid_adherent
        WHERE  A.id_adherent = 'xxxx'
        AND actif = 1
        

        bien sûr.. tu remplace les xxxx par un ID d'adhérant existant dans ta BDD
    2. SUPER génial ça marche tu peux me donner une explication entre LEFT et INNER s'il te plait.
      Merci encore
      1. Modérateur
        https://www.commentcamarche.net/faq/34631-mysql-quelle-jointure-choisir