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 :

Problème d'une requête


Sujet :

Langage SQL

  1. #1
    Futur Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Problème d'une requête
    Bonjour tous !
    Je dois faire un requête selon des conditions.
    J'ai fais cette condition mais elle est trop longue, y-a-t-il une façon plus simple pour l'écrire:
    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
    28
    29
    30
     
           If nomtxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Nom='" + nomtxt.Text + "'"
     
            ElseIf prentxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Prenom='" + prentxt.Text + "'"
     
            ElseIf soctxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Libelle5='" + soctxt.Text + "'"
     
            ElseIf nomtxt.Text <> "" And prentxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Nom='" + nomtxt.Text + "' and Prenom='" + prentxt.Text + "'"
     
            ElseIf nomtxt.Text <> "" And soctxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Nom='" + nomtxt.Text + "' and Libelle5='" + soctxt.Text + "'"
     
            ElseIf prentxt.Text <> "" And soctxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Prenom='" + prentxt.Text + "' and Libelle5='" + soctxt.Text + "'"
     
            ElseIf nomtxt.Text <> "" And soctxt.Text <> "" And prentxt.Text <> "" Then
     
                sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE WHERE Nom='" + nomtxt.Text + "' and Prenom='" + prentxt.Text + "' and Libelle5='" + soctxt.Text + "'"
     
            End If
    prentxt, nomtxt et soctxt se sont des textbox.
    Merci d'avance

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Responsable Données
    Inscrit en
    Janvier 2009
    Messages
    5 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Responsable Données

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5 198
    Points : 12 774
    Points
    12 774
    Par défaut
    Bonjour,
    Tu peux préparer la première partie de la requête dans une chaine (juste les clauses Select et From).
    Ensuite tu testes chaque textbox, et si elles ont une valeur tu ajoutes la condition dans une autre variable.

    Enfin si la variable "condition" contient quelquechose, tu l'ajoutes avec un Where à la première partie de la requête.

    Ainsi tu n'as pas besoin de tester chaque combinaison de critères, ce qui de toute façon deviendra vite ingérable...

    Tatayo.

    P.S. à mon avis ce n'est pas la bonne section, car c'est plus un problème de conception que de SQL.

  3. #3
    Futur Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut
    En fait, je n'ai pas bien compris la solution que tu m'as proposée, si tu peux me bien expliquer je l'apprécierai de ta part.
    Merci beaucoup.

  4. #4
    Expert éminent sénior
    Homme Profil pro
    Responsable Données
    Inscrit en
    Janvier 2009
    Messages
    5 198
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Responsable Données

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5 198
    Points : 12 774
    Points
    12 774
    Par défaut
    Je vois un truc du genre:
    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
     
    sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE"
     
    IF nomtxt.Text <> "" Then
    	condition = " Nom='" + nomtxt.Text + "'"
    endif
     
    IF prenmtxt.Text <> "" Then
    	if condition <> "" then
    		condition = condition + " and "
    	endif
    	condition = " Prenom='" + Prentxt.Text + "'"
    endif
     
    IF soctxt.Text <> "" Then
    	if condition <> "" then
    		condition = condition + " and "
    	endif
    	condition = " libelle5='" + soctxt.Text + "'"
    endif
     
    IF condition <> "" then
    	sql = sql + " where " + condition
    endif
    Tatayo.

  5. #5
    Futur Membre du Club
    Profil pro
    Développeur informatique
    Inscrit en
    Janvier 2011
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 8
    Points : 6
    Points
    6
    Par défaut
    Merci pour votre aide.J'ai édité le code que vous m'avez donné.
    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
     
    sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE "
     
            If nomtxt.Text <> "" Then
                condition = " Nom='" + nomtxt.Text + "'"
                End if
            If prentxt.Text <> "" Then
                If condition <> "" Then
                    condition = condition + " and Prenom='" + prentxt.Text + "'"
                Else
                    condition = " Prenom='" + prentxt.Text + "'"
                End If
            End If
            If soctxt.Text <> "" Then
                If condition <> "" Then
                    condition = condition + " and libelle5='" + soctxt.Text + "'"
                Else
                    condition = " libelle5='" + soctxt.Text + "'"
                End If
            End If
            If condition <> "" Then
                sql = sql + " where " + condition
            End If
    et à la fin de ma recherche je remets condition à zéro pour pouvoir la réutiliser
    Merci beaucoup cordialement.

  6. #6
    Membre confirmé Avatar de juvamine
    Profil pro
    Chef de projet MOA
    Inscrit en
    Mai 2004
    Messages
    414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Chef de projet MOA
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Mai 2004
    Messages : 414
    Points : 502
    Points
    502
    Par défaut
    Il y a plus simple, pour éviter de savoir s'il faut mettre where ou pas
    Tu commences ton where par une condition toujours vrai
    et ensuite tu ne mets que des AND

    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    sql = "SELECT Nom,Prenom,Libelle5 as Société,Photo from MSESAME.MS_BADGE "
    condition = "WHERE 1 = 1 "
     
            IF nomtxt.Text <> "" Then
                condition = condition & " AND Nom='" + nomtxt.Text + "'"
            End IF
            IF prentxt.Text <> "" Then
                    condition = condition & " and Prenom='" + prentxt.Text + "'"            
            End IF
            IF soctxt.Text <> "" Then
                    condition = condition + " and libelle5='" + soctxt.Text + "'"            
            End IF
     
           sql = sql + condition

    ça permet d'alléger un peu le code...

    Bon courage

    juva
    Juvamine

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

Discussions similaires

  1. Problème avec une requête
    Par ringostarr dans le forum Langage SQL
    Réponses: 5
    Dernier message: 19/04/2005, 20h34
  2. Problème avec une requête
    Par snoopy69 dans le forum Débuter
    Réponses: 2
    Dernier message: 20/01/2005, 12h39
  3. problème avec une requête imbriquée
    Par jaimepasteevy dans le forum Langage SQL
    Réponses: 13
    Dernier message: 05/12/2003, 10h29
  4. Problème sur une requête INSERT
    Par Marion dans le forum Langage SQL
    Réponses: 3
    Dernier message: 17/06/2003, 08h45
  5. problème sur une requête!!!!!
    Par Mcgrady_01 dans le forum Langage SQL
    Réponses: 2
    Dernier message: 13/06/2003, 01h17

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