Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1561 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  Problème jointure

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Problème jointure

n°2291218
kewan
Posté le 06-11-2016 à 22:31:34  profilanswer
 

:hello:  tout le forum.
 
J'ai besoin de votre aide.
 
J'ai 3 tables :
 
http://img15.hostingpics.net/pics/151602Image1.jpg
 
Je voudrais connaitre pour chaque travaux le nom des agents qui y sont assignés.
 
Je ne sais pas comment écrire que les champs id_agent1, id_agent2 et id_agent3 sont = agent.id
 
Je ne sais pas comment lire les données.
 
Voilà ce que j'ai fait pour l'instant :
 

Code :
  1. $cherche_agent = $bdd->query
  2. ('
  3.    SELECT        repartition.id_agent1, repartition.id_agent2, repartition.id_agent3,
  4.                  travaux.nom_travaux,
  5.                  agent.nom_agent
  6.    FROM          repartition
  7.    INNER JOIN    travaux
  8.    INNER JOIN    agent
  9.    ON            repartition.id_travaux = '.$_POST ['demande'].'
  10.    AND           repartition.id_travaux = travaux.id
  11.    AND           repartition.id_agent1 = agent.id
  12.    AND           repartition.id_agent2 = agent.id
  13.    AND           repartition.id_agent3 = agent.id
  14. ');
  15. $agent = $cherche_agent->fetch();
  16. echo $agent['nom_travaux'].$agent['nom_agent'].$agent['nom_agent'].$agent['nom_agent'];


Comment indiquer les liens ?

Code :
  1. repartition.id_agent1 = agent.id
  2. repartition.id_agent2 = agent.id
  3. repartition.id_agent3 = agent.id

Comment lire les données ?

Code :
  1. echo $agent['nom_travaux'].$agent['nom_agent'].$agent['nom_agent'].$agent['nom_agent'];

Merci pour votre aide
 
kewan

mood
Publicité
Posté le 06-11-2016 à 22:31:34  profilanswer
 

n°2291232
mechkurt
Posté le 07-11-2016 à 15:09:23  profilanswer
 

Code :
  1. $cherche_agent = $bdd->query
  2. ('
  3.    SELECT        repartition.id_agent1, repartition.id_agent2, repartition.id_agent3,
  4.                  travaux.nom_travaux,
  5.                  a1.nom_agent AS agent1,
  6.                  a2.nom_agent AS agent2,
  7.                  a3.nom_agent AS agent3
  8.    FROM          repartition
  9.    INNER JOIN    travaux
  10.    INNER JOIN    agent a1 ON repartition.id_agent1 = a1.id
  11.    INNER JOIN    agent a2 ON repartition.id_agent2 = a2.id
  12.    INNER JOIN    agent a3 ON repartition.id_agent3 = a3.id
  13.    WHERE         repartition.id_travaux = '.$_POST ['demande'].'
  14.    AND  repartition.id_travaux = travaux.id
  15. ');


Essayes plutôt un truc du genre (avec des left join au lieu des inner join si l'id_agent peut ne pas être définis).


---------------
D3
n°2291286
rufo
Pas me confondre avec Lycos!
Posté le 08-11-2016 à 13:19:10  profilanswer
 

On peut déjà faire remarquer que la BD est très mal modélisée :/ Je parle surtout de la table "répartition". Il aurait mieux valu faire une relation n-n entre les agents et les travaux.
Ex : repartition : id_repartition, id_travaux, id_agent
 
Sinon, on peut faire une requête avec des UNION
SELECT a1.nom_agent, t1.nom_travaux FROM agent a1 INNER JOIN repartition r ON (a1.id_agent = r.id_agent1) INNER JOIN travaux t1 ON (r.id_travaux = t1.id_travaux) WHERE r.id_travaux = '.$_POST ['demande']
UNION
SELECT a2.nom_agent, t2.nom_travaux FROM agent a2 INNER JOIN repartition r ON (a2.id_agent = r.id_agent2) INNER JOIN travaux t2 ON (r.id_travaux = t2.id_travaux) WHERE r.id_travaux = '.$_POST ['demande']
UNION
SELECT a3.nom_agent, t3.nom_travaux FROM agent a3 INNER JOIN repartition r ON (a3.id_agent = r.id_agent3) INNER JOIN travaux t3 ON (r.id_travaux = t3.id_travaux) WHERE r.id_travaux = '.$_POST ['demande']


---------------
Astres, outil de help-desk GPL : http://sourceforge.net/projects/astres, ICARE, gestion de conf : http://sourceforge.net/projects/icare, Outil Planeta Calandreta : https://framalibre.org/content/planeta-calandreta
n°2291635
kewan
Posté le 12-11-2016 à 10:35:22  profilanswer
 

:hello: Merci pour vos réponses.
 
Je vais revoir ma base de données et faire une table jointure pour lier les travaux et les agents le code sera plus claire et flexible, et la maintenance sera plus facile :
https://user.oc-static.com/upload/2016/11/07/14785178208572_Image1.jpg
 
j'ai fait des test avec la requête de mechkurt. En mettent des alias pour toutes les tables,
 

Code :
  1. $cherche_agent = $bdd->query
  2.     ('
  3.        SELECT        r.id_agent1, r.id_agent2, r.id_agent3,
  4.                      t.nom_travaux,
  5.                      a1.nom_agent AS agent1,
  6.                      a2.nom_agent AS agent2,
  7.                      a3.nom_agent AS agent3
  8.        FROM          repartition r
  9.        INNER JOIN    travaux t ON r.id_travaux = t.id
  10.        INNER JOIN    agent a1  ON r.id_agent1 = a1.id
  11.        INNER JOIN    agent a2  ON r.id_agent2 = a2.id
  12.        INNER JOIN    agent a3  ON r.id_agent3 = a3.id
  13.        AND           r.id_travaux = '.$_POST ['demande'].'
  14.     ');
  15. $agent = $cherche_agent->fetch();


 
Elle fonctionne sauf si dans la table repartition un des agents n'est pas affecté. Il y a donc un zero pour son id.
 
La requête cherche le nom de l'agent associé à l'id 0.
 
J'ai testé :

Code :
  1. for($i = 1;$i <= 3;$i++)
  2. {
  3.      if (id_agent.$i != 0)
  4.      {
  5.           echo $agent['agent'.$i].'<br>;
  6.      }
  7. }


Message édité par kewan le 12-11-2016 à 10:48:21
n°2291646
rufo
Pas me confondre avec Lycos!
Posté le 12-11-2016 à 12:36:52  profilanswer
 

Comme indiqué, ta structure de BD n'est vraiment pas adapté : si y'a moins de 3 agents, tu dois bidouiller et si y'en a plus, tu ne peux pas gérer. Une table travaux_agents avec un id, id_gent et id_travaux serait bien plus flexible et te faciliterait grandement l'écriture de tes requêtes SQL ;) En général, quand une requête est compliquée à faire (je parle de bidouilles en SQL), c'est qu'on a un truc qui ne va pas dans la structure de la BD pour faire le traitement voulu...


---------------
Astres, outil de help-desk GPL : http://sourceforge.net/projects/astres, ICARE, gestion de conf : http://sourceforge.net/projects/icare, Outil Planeta Calandreta : https://framalibre.org/content/planeta-calandreta

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  Problème jointure

 

Sujets relatifs
Problème jointure requête sql.[RESOLU] Plusieurs "like" de suite + probleme jointure
probleme requete sql jointure entre les tablesProbleme de jointure sur une seule table sql
[ACCESS] Problème jointure entre requêtesPostgresl, probleme de jointure
[RESOLU][SQL/ACCESS] Problème jointureProblème Jointure SQL
problème de jointure dans ma requetteProblème de choix de jointure
Plus de sujets relatifs à : Problème jointure


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR