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

 MySQL Discussion :

Requete SQL UNION


Sujet :

MySQL

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Par défaut Requete SQL UNION
    Bonjour
    j'essaye de récupérer des valeurs pour une la date X et pour la date Y je crois que pour cela je suis obligé d'utiliser l'UNION

    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
    (SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col1
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-25')
    UNION ALL
    (SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col2
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-26')
    sauf que col2 ne s'affiche pas et je sais pas pourquoi :s

  2. #2
    Membre émérite
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    445
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2011
    Messages : 445
    Par défaut
    Ton union est équivalente à cette requete:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col1
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND (rank_rapport.date_position = '2012-07-25'
    							  OR rank_rapport.date_position = '2012-07-26')
    Tu peux faire comme ça, mais c'est pas terrible :
    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
    (SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col1,null AS col2
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-25')
    UNION ALL
    (SELECT kw.kw , kw.id_kw , kw.url_destination , null AS col1, rank_rapport.position AS col2
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-26')
    Ou comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    SELECT kw.kw , kw.id_kw , kw.url_destination , CASE rank_rapport.date_position WHEN '2012-07-25' THEN rank_rapport.position ELSE null END  AS col1,
                                                   CASE rank_rapport.date_position WHEN '2012-07-26' THEN rank_rapport.position ELSE null END  AS col2
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND (rank_rapport.date_position = '2012-07-25'
    							  OR rank_rapport.date_position = '2012-07-26')

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Par défaut
    Il n'est pas possible d'avoir rank_rapport.position pour la date X et rank_rapport.position pour la date Y en parallèle ?

  4. #4
    Membre émérite
    Homme Profil pro
    Inscrit en
    Juin 2011
    Messages
    445
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Juin 2011
    Messages : 445
    Par défaut
    Je ne sais pas si ça va fonctionner et si c'est la bonne façon de faire, mais tu peux toujours essayer ça :
    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
    SELECT t1.kw,t1.id_kw,t1.url_destination,col1,col2 FROM (
    (SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col1
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-25') t1
    JOIN 
    (SELECT kw.kw , kw.id_kw , kw.url_destination , rank_rapport.position AS col2
    							FROM prestation
    							JOIN kw ON kw.id_prestation = prestation.id_prestation
    							JOIN rank_conf ON rank_conf.id_prestation = prestation.id_prestation
    							JOIN rank_rapport ON rank_rapport.id_kw = kw.id_kw 
    							WHERE prestation.id_prestation = '185'
    							AND rank_rapport.date_position = '2012-07-26') t2
    ON t1.kw= t2.kw AND t1.id_kw=t2.id_kw AND t1.url_destination=t2.url_destination
    )

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Par défaut
    Ah bah c'est ce que je recherchais , je vais vraiment me plonger dans le langage SQL ^^
    Merci !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Syntaxe requete sql UNION dans le code VBA
    Par fisio dans le forum VBA Access
    Réponses: 3
    Dernier message: 15/11/2010, 13h44
  2. Optimisation de requetes SQL UNION
    Par thierry_78 dans le forum Requêtes
    Réponses: 1
    Dernier message: 18/06/2010, 09h12
  3. compter enregistrement dans une requete sql avec UNION
    Par dbzzzde dans le forum VBA Access
    Réponses: 2
    Dernier message: 24/10/2007, 10h43
  4. PL/SQL requete avec UNION
    Par lapartdombre dans le forum PL/SQL
    Réponses: 3
    Dernier message: 17/11/2005, 08h40
  5. Union de requetes sql
    Par bejaad dans le forum Décisions SGBD
    Réponses: 1
    Dernier message: 26/05/2005, 17h42

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