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

  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  [RÉGLÉ] [SQL] Jointure pour afficher un avatar

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[RÉGLÉ] [SQL] Jointure pour afficher un avatar

n°2303865
tomware
Moteur à eau
Posté le 26-07-2017 à 22:09:59  profilanswer
 

Bonjour  :hello:  
 
Je voudrais savoir si une requête est possible et j'ai besoin de votre aide  :jap:  
 
Mon site a un systeme de votes et je veux afficher l'avatar des gens qui ont voté pour quelqu'un.  
Sauf que :
- des membres n'ont pas d'avatar (et j'aimerai en afficher un par defaut pour ceux la)
- les membres qui n'ont pas d'avatar ne sont pas dans la table image_avatar (c'était voulu)
 
J'ai 2 tables :
vote : id_electeur | id_candidat
avatar : membre_id | nom_fichier
 
Je fais donc une jointure :
 

Code :
  1. $sql = 'select 
  2. a.id_electeur, a.id_candidat,
  3. b.membre_id, b.nom_fichier
  4. FROM vote a
  5. JOIN avatar b ON a.id_electeur = b.membre_id
  6. WHERE a.id_candidat = "'.$id_consultation_en_cours_membre.'"
  7. LIMIT 8
  8. ';
  9. $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
  10. while($data = mysql_fetch_array($req)){
  11. $nom_fichier = $data['nom_fichier'];
  12. $url_photo = "http://monsite.com/$nom_fichier"
  13. if ($nom_fichier == '')  { $url_photo = "http://monsite.com/img/avatars/defaut.png"; }
  14. echo " <img src='$url_photo'> ";
  15. }


 
Mais donc ben la jointure ca sélectionne que ceux qui ont 1 avatar ET qui ont voté  
 [:tomware]  
Alors c'est normal que ca n'affiche pas ceux qui n'ont pas d'avatar et qui ont voté  
 [:tomware]  
Et comme je suis noob j'ai aucune idée de comment je pourrais afficher l'avatar par défaut de ces gens qui ont voté (sachant que j'ai leur id membre dans la table vote)  
 [:tomware]  
 
Merci d'avance pour votre coup de main  :jap:


Message édité par tomware le 29-07-2017 à 10:56:11
mood
Publicité
Posté le 26-07-2017 à 22:09:59  profilanswer
 

n°2303966
alokkin
tourner 7 fois la langue ...
Posté le 28-07-2017 à 15:02:26  profilanswer
 

En fait, quand tu écris "join", par défaut, ton SGBDR réalise une jointure interne (INNER JOIN). La solution se trouve dans la jointure externe :  
select  
  a.id_electeur,  
  a.id_candidat,
  b.membre_id,  
  b.nom_fichier
FROM  
  vote a
  LEFT OUTER JOIN avatar b  
    ON a.id_electeur = b.membre_id
WHERE  
  a.id_candidat = "'.$id_consultation_en_cours_membre.'"
LIMIT 8


---------------
Burn the museum, wipe your ass with the Mona Lisa, this way, at least, God will know your name.
n°2303996
tomware
Moteur à eau
Posté le 29-07-2017 à 10:55:56  profilanswer
 

Super génial ca marche merci a toi Alokkin  :jap:  
 
 :hello:


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  SQL/NoSQL

  [RÉGLÉ] [SQL] Jointure pour afficher un avatar

 

Sujets relatifs
INDEX est obligatoire pour afficher contenu de la table ?Afficher périodiquement et aléatoirement une valeur d'un array
Afficher une suite de nombre avec un char ou int ?Exercice de la piscine 42: afficher l'alphabet
[HTML/PHP/JS] Afficher x pages en alternances sur timerAfficher une grille dans GoogleEarth à partir d'un point
[Wordpress] Afficher article(s) sur FacebookProblème jointure
Afficher plusieurs lignes sur une seuleOpenLayers3 : afficher Text
Plus de sujets relatifs à : [RÉGLÉ] [SQL] Jointure pour afficher un avatar


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