Précédent   Forum des professionnels en informatique > Bases de données > MySQL > Débuter
Débuter Forum d'entraide pour débuter avec MySQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 04/01/2011, 15h32   #1
Membre habitué
 
Avatar de tintin72
 
Inscription : septembre 2003
Messages : 595
Détails du profil
Informations forums :
Inscription : septembre 2003
Messages : 595
Points : 123
Points : 123
Par défaut Condition IF. Help!

Bonjour,

J'aurais besoin d'une condition IF dans ma requête mais je ne connais pas la
syntaxe.
Voilà ma requête:
Code :
1
2
3
4
5
6
7
8
SELECT ta.*, 
    tb.title AS tb_title, 
    tc.title AS tc_title, 
    td.title AS td_title,
FROM table_a AS ta 
INNER JOIN table_b AS tb ON tb.id = ta.tb_id    
INNER JOIN table_c AS tc ON tc.id = ta.tc_id 
INNER JOIN table_d AS td ON td.id = ta.td_id;
Ce que je voudrais c'est que le dernier INNER JOIN ne soit exécuté que si ta.td_id est différent de zéro, sinon td_title vaut 'none'.

Quelqu'un pourrait-il m'aider ?

Merci.
tintin72 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2011, 16h43   #2
Modérateur
 
Avatar de CinePhil
 
Homme Philippe Leménager
Ingénieur d'études en informatique
Inscription : août 2006
Messages : 10 980
Détails du profil
Informations personnelles :
Nom : Homme Philippe Leménager
Âge : 48
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Ingénieur d'études en informatique
Secteur : Enseignement

Informations forums :
Inscription : août 2006
Messages : 10 980
Points : 18 224
Points : 18 224
Envoyer un message via MSN à CinePhil
On ne peut pas faire une jointure conditionnelle.
Si tu veux dire que certaines lignes de table_a n'ont pas de correspondant dans la table_d, alors c'est une jointure externe qu'il te faut :
Code :
1
2
3
4
5
6
7
8
SELECT ta.*, 
    tb.title AS tb_title, 
    tc.title AS tc_title, 
    td.title AS td_title,
FROM table_a AS ta 
INNER JOIN table_b AS tb ON tb.id = ta.tb_id    
INNER JOIN table_c AS tc ON tc.id = ta.tc_id 
LEFT OUTER JOIN table_d AS td ON td.id = ta.td_id;
S'il n'y a pas de correspondance, td_title affichera NULL.
Si tu veux afficher autre chose que NULL, il faut utiliser COALESCE :
Code :
1
2
3
4
5
6
7
8
SELECT ta.*, 
    tb.title AS tb_title, 
    tc.title AS tc_title, 
    COALESCE(td.title, 'none') AS td_title,
FROM table_a AS ta 
INNER JOIN table_b AS tb ON tb.id = ta.tb_id    
INNER JOIN table_c AS tc ON tc.id = ta.tc_id 
LEFT OUTER JOIN table_d AS td ON td.id = ta.td_id;
__________________
Philippe Leménager. Ingénieur d'étude à l'École Nationale de Formation Agronomique.
Mon blog sur la conception des BDD, le langage SQL, le PHP avec Zend Framework...
« Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément ». (Nicolas Boileau)
À la maison comme au bureau, j'utilise Mandriva Linux ou Mageïa ! Soutenons l'industrie logicielle française !
Linuxiens, comptez-vous !
CinePhil est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/01/2011, 17h15   #3
Membre habitué
 
Avatar de tintin72
 
Inscription : septembre 2003
Messages : 595
Détails du profil
Informations forums :
Inscription : septembre 2003
Messages : 595
Points : 123
Points : 123
Super, Merci!
Je galérais depuis un moment avec le IF.
tintin72 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 13h54.


 
 
 
 
Partenaires

Hébergement Web