IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage SQL Discussion :

Condition WHERE CASE


Sujet :

Langage SQL

  1. #1
    Nouveau Candidat au Club
    Profil pro
    Inscrit en
    Août 2010
    Messages
    1
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Condition WHERE CASE
    Bonsoir,

    La requete suivante s'execute mais ne retourne rien, alors qu'elle devrait. Ca va faire 2 jours que je cherche une solution sans succès, pourriez vous m'aider ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
    <?php
    $Query = $Sql->query('SELECT mp_threads.mp_thread_id, 
    	mp_threads.mp_thread_starter_id, 
    	mp_threads.mp_thread_dest_id, 
    	mp_threads.mp_thread_subject, 
    	mp_threads.mp_thread_starter_state, 
    	mp_threads.mp_thread_dest_state, 
    	mp_threads.mp_thread_date, 
    	CASE WHEN mp_threads.mp_thread_starter_id = '.$_SESSION['mbr_id'].' THEN "starter" ELSE "dest" END AS mbr_role, 
    	members_starter.mbr_id AS mbr_id_starter, 
    	members_starter.mbr_pseudo AS mbr_pseudo_starter,
    	members_dest.mbr_id AS mbr_id_dest, 
    	members_dest.mbr_pseudo AS mbr_pseudo_dest 
    	FROM mp_threads 
    	LEFT JOIN members AS members_starter
    	ON members_starter.mbr_id = mp_threads.mp_thread_starter_id 
    	LEFT JOIN members AS members_dest 
    	ON members_dest.mbr_id = mp_threads.mp_thread_dest_id
    	WHERE (CASE WHEN mp_threads.mp_thread_starter_id = '.$_SESSION['mbr_id'].' THEN
    		mp_thread_starter_state != 2 
    	ELSE 
    		mp_thread_dest_state != 2 
    	END) 
    	AND (mp_thread_starter_id = '.$_SESSION['mbr_id'].' OR mp_thread_dest_id = '.$_SESSION['mbr_id'].') 
    	ORDER BY mp_thread_date DESC') or exit(print_r($Query->errorInfo()));
    ?>
    Merci !

  2. #2
    Membre du Club
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Août 2010
    Messages
    44
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Août 2010
    Messages : 44
    Points : 69
    Points
    69
    Par défaut
    Bonsoir,


    Dans votre clause WHERE, il y a :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    ...
      WHERE (
           CASE
                WHEN mp_threads.mp_thread_starter_id = '.$_SESSION['mbr_id'].
                THEN mp_thread_starter_state        != 2
                ELSE mp_thread_dest_state           != 2
           END)
    ...
    Qui est assez inhabituel

    J'ecrirai plutôt :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    ...
      WHERE ( (mp_threads.mp_thread_starter_id = '.$_SESSION['mbr_id'].'
                     AND  mp_thread_starter_state        != 2)
                OR (mp_threads.mp_thread_starter_id != '.$_SESSION['mbr_id'].'
                     AND  mp_thread_dest_state           != 2))
    ...
    Cdlt,
    OD

  3. #3
    Modérateur
    Avatar de al1_24
    Homme Profil pro
    Retraité
    Inscrit en
    Mai 2002
    Messages
    9 109
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Retraité
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 9 109
    Points : 28 434
    Points
    28 434
    Par défaut
    Une autre manière de l'écrire
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    WHERE   CASE
            WHEN mp_threads.mp_thread_starter_id = '.$_SESSION['mbr_id'].'
                THEN mp_thread_starter_state
            ELSE mp_thread_dest_state
            END <> 2
    Ou encore
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    WHERE   CASE mp_threads.mp_thread_starter_id
            WHEN '.$_SESSION['mbr_id'].'
                THEN mp_thread_starter_state
            ELSE mp_thread_dest_state
            END <> 2

Discussions similaires

  1. Réponses: 3
    Dernier message: 16/02/2006, 16h58
  2. probleme gestion condition switch - case
    Par DarkMax dans le forum Langage
    Réponses: 5
    Dernier message: 07/09/2005, 14h25
  3. Condition WHERE
    Par adilou1981 dans le forum Langage SQL
    Réponses: 5
    Dernier message: 04/11/2004, 11h04
  4. Fonction MAX dans une condition WHERE
    Par borgfabr dans le forum Langage SQL
    Réponses: 4
    Dernier message: 12/10/2004, 16h06
  5. Réponses: 6
    Dernier message: 24/09/2004, 13h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo