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

Requêtes MySQL Discussion :

traduire une requête faite sous Access


Sujet :

Requêtes MySQL

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut traduire une requête faite sous Access
    Bonjour
    C'est mon premier et peut être dernier post car je ne suis pas du tout informaticien, soyez indulgents et surtout très clairs
    J'ai réalisé tant bien que mal une requête entre deux bases MySql 4-1. Elle fonctionne mais j'ai des soucis de lien ODBC instable. Je voudrai donc la ré-écrire en SQL mais j'en suis incapable et les manuels me balladent dans tous les sens.

    La requête Access :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    INSERT INTO DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality )
    SELECT [wrk_file_attente].[code_file], [wrk_file_attente].[code_patient],
    [glb_patient]![nom]+"^"+[glb_patient]![prenom] AS Expr1, 
    Format([glb_patient]![date_naissance],"yyyymmdd") AS Expr4, "F" AS Expr2, 
    Format([wrk_file_attente]![heure_arrivee],"yyyymmdd") AS Expr5, "US" AS Expr3
    FROM wrk_file_attente 
      INNER JOIN glb_patient ON [wrk_file_attente].[code_patient]=[glb_patient].[code_patient]
    WHERE ((([wrk_file_attente].[code_medecin])=15 
      Or ([wrk_file_attente].[code_medecin])=28 
      Or ([wrk_file_attente].[code_medecin])=85));
    AVEC :
    `dicom`.`dicomworklist`.`AccessionN` = `gyndata`.`code_file` (AccessionN et code_file sont tous les deux des clés primaires)
    `dicom`.`dicomworklist`.`PatientNam` = `gyndata`.`glb_patient.nom`+"^"+`gyndata`.`glb_patient.prenom`
    `dicom`.`dicomworklist`.`PatientBir` = `gyndata`.`glb_patient.date_naissance` au format YYYYMMDD
    `dicom`.`dicomworklist`.`PatientSex` = "F"
    `dicom`.`dicomworklist`.`Modality` = "US"
    `dicom`.`dicomworklist`.`Startdate` =
    `gyndata`.`wrk_file_attente.heure_arrivee` au format YYYYMMDD

    Je voudrai traduire cette requète en SQL, l'automatiser et surtout lui ajouter une condition : Ignorer INSERT si la patiente a déjà été importée : sinon il y a un conflit de clé primaire.

    Merci de votre aide.

  2. #2
    Rédacteur/Modérateur

    Avatar de Antoun
    Homme Profil pro
    Architecte décisionnel
    Inscrit en
    Octobre 2006
    Messages
    6 284
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Architecte décisionnel
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2006
    Messages : 6 284
    Points : 11 737
    Points
    11 737
    Par défaut
    ça devrait le faire comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    INSERT IGNORE INTO DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality )
    SELECT wrk_file_attente.code_file, wrk_file_attente.code_patient,
      CONCAT(glb_patient.nom, '^', glb_patient.prenom), 
      DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') , 
      'F', 
      DATE_FORMAT(wrk_file_attente.heure_arrivee,'%Y%m%d'), 
      'US' AS Expr3
    FROM wrk_file_attente 
      INNER JOIN glb_patient ON wrk_file_attente.code_patient=glb_patient.code_patient
    WHERE wrk_file_attente.code_medecin IN (15, 28, 85) ;
    Le mot-cle IGNORE indique à MySQL d'ignorer les lignes qui provoquent des erreurs, comme par exemple un conflit de clé primaire.

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    D'abord grand merci.
    Au premier essai, j'ai eu un message "no database selected".
    J'ai rajouté un peu au hasard les noms des deux bases gyndata et dicom comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    INSERT IGNORE INTO dicom.DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality )
    SELECT wrk_file_attente.code_file, wrk_file_attente.code_patient,
      CONCAT(glb_patient.nom, '^', glb_patient.prenom),
      DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
      'F',
      DATE_FORMAT(wrk_file_attente.heure_arrivee,'%Y%m%d'),
      'US' AS Expr3
    FROM gyndata.wrk_file_attente
      INNER JOIN gyndata.glb_patient ON wrk_file_attente.code_patient=glb_patient.code_patient
    WHERE gyndata.wrk_file_attente.code_medecin IN (15, 28, 85) ;
    Ca marche... Est ce que c'est correct comme syntaxe ?
    De quel coté, faut il que je cherche pour automatiser cette requête de façon à ce qu'elle s'execute toutes les 30 secondes ? php ?

  4. #4
    Rédacteur/Modérateur

    Avatar de Antoun
    Homme Profil pro
    Architecte décisionnel
    Inscrit en
    Octobre 2006
    Messages
    6 284
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Architecte décisionnel
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2006
    Messages : 6 284
    Points : 11 737
    Points
    11 737
    Par défaut
    Citation Envoyé par alain_godard Voir le message
    Ca marche... Est ce que c'est correct comme syntaxe ?
    oui
    Citation Envoyé par alain_godard Voir le message
    De quel coté, faut il que je cherche pour automatiser cette requête de façon à ce qu'elle s'execute toutes les 30 secondes ? php ?
    Pas dans MySQL 4.1 en tout cas...
    Le plus simple est d'enregistrer ta requête dans un fichier appelé par exemple copie.sql, et (sous Windows) de créer un fichier copie.bat qui contienne un code de ce genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    mysql.exe -utonlogin -ptonmotdepasse < copie.sql
    Il ne te reste ensuite plus qu'à programmer l'exécution de copie.bat toutes les 30 secondes...

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Merci, c'est impreccable, Les tâches planifiées de windows ne proposent pas la répétition toutes les 30 secondes. Mais ce n'est pas le sujet de ce forum. Merci encore.

  6. #6
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    J'ai modifié la requète initiale car elle ne répondait pas à mon attente et malgré tous mes efforts, je butte sur la syntaxe..

    Une base dicom à mettre à jour et une base gyndata source des informations dans les tables trc_dossier et glb_patient :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    INSERT IGNORE INTO dicom.DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient, 
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
      'US' AS Expr3
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS Null;
    Merci de votre aide.

    OUPSSS Excusez moi, cette requète fonctionne après mise à jour de query browser....

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    J'ai encore un problème, toujours sur cette même requête : j'ai besoin d'insérer dans le champs "lastmenst", table "worklist", base "dicom". La date renseignée par le champ "date_derniere_regle", table "sa_grossesse", base "gyndata"...
    Mais je n'arrive pas à ordonner correctement les jointures.
    La requete Access fonctionne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    INSERT INTO DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality, LastMenstr )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient, [glb_patient]![nom]+"^"+[glb_patient]![prenom] AS Expr1, Format([glb_patient]![date_naissance],"yyyymmdd") AS Expr4, "F" AS Expr2, Format([trc_dossier]![date_entree],"yyyymmdd") AS Expr5, "US" AS Expr3, Format([sa_grossesse]![date_derniere_regle],"yyyymmdd") AS Expr6
    FROM (glb_patient INNER JOIN trc_dossier ON glb_patient.code_patient = trc_dossier.code_patient) INNER JOIN sa_grossesse ON glb_patient.code_patient = sa_grossesse.code_patient
    WHERE (((trc_dossier.station) Like "STA_MAT_033" Or (trc_dossier.station) Like "STA_MAT_019") AND ((trc_dossier.date_entree)>=Date()) AND ((trc_dossier.date_sortie) Is Null));
    Mais je plante sur la traduction SQL (MySql 4-1) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    INSERT IGNORE INTO dicom.DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality, LastMenstr )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient,
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
       'US' AS Expr3,
       DATE_FORMAT(sa_grossesse.date_derniere_regle,'%Y%m%d')
    FROM gyndata.trc_dossier, gyndata.sa_grossesse,
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient,
       INNER JOIN gyndata.glb_patient ON sa_grossesse.code_patient = glb_patient.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS Null;
    Visiblement je ne range pas bien les INNER JOIN......

  8. #8
    Rédacteur/Modérateur

    Avatar de Antoun
    Homme Profil pro
    Architecte décisionnel
    Inscrit en
    Octobre 2006
    Messages
    6 284
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Architecte décisionnel
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2006
    Messages : 6 284
    Points : 11 737
    Points
    11 737
    Par défaut
    a priori, chaque table doit être jointurée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    FROM table1
      INNER JOIN table2 ON condition t1/t2
      INNER JOIN table3 ON condition t1/t3 ou t2/t3
    ...
    ne pas jointurer une table, ça veut dire faire un produit cartésien.

  9. #9
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    INSERT IGNORE INTO dicom.DICOMWorkList
    (AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality, LastMenstr )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient,
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
       'US',
       DATE_FORMAT(sa_grossesse.date_derniere_regle,'%Y%m%d')
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
       INNER JOIN gyndata.sa_grossesse ON glb_patient.code_patient = sa_grossesse.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS Null;
    Ca marche ! Merci, merci, merci.....

  10. #10
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Décidement, à chaque fois je coche "Résolu" persuadé que mon problème est définitivement réglé et à chaque fois j'ai encore un bug...
    La requête dans le message précédent ne fonctionne pas si le champ : "sa_grossesse.date_derniere_regle" est vide. La patiente n'est pas ajoutée à la worklist dicom. Ce qui ne me va pas du tout !
    J'ai corrigé le problème en mettant dans mon script sql les deux requêtes à la suite : celle qui renseigne les dernières règles et la précédente qui n'en tenait pas compte. Ca marche semble t il mais est ce bien la bonne solution....
    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
    INSERT IGNORE INTO dicom.DICOMWorkList
    (AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality, LastMenstr )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient,
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
       'US',
       DATE_FORMAT(sa_grossesse.date_derniere_regle,'%Y%m%d')
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
       INNER JOIN gyndata.sa_grossesse ON glb_patient.code_patient = sa_grossesse.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS Null;
    INSERT IGNORE INTO dicom.DICOMWorkList ( AccessionN, PatientID, PatientNam, PatientBir, PatientSex, StartDate, Modality )
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient, 
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
      'US' AS Expr3
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS Null;

  11. #11
    Rédacteur/Modérateur

    Avatar de Antoun
    Homme Profil pro
    Architecte décisionnel
    Inscrit en
    Octobre 2006
    Messages
    6 284
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Architecte décisionnel
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2006
    Messages : 6 284
    Points : 11 737
    Points
    11 737
    Par défaut
    Je ne comprends pas assez ce que tu fais... Que donnent tes SELECT si tu les sors de leurs INSERT ? Quelles sont les résultats qui te semblent incorrects, et en quoi ?

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient,
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
       'US',
       DATE_FORMAT(sa_grossesse.date_derniere_regle,'%Y%m%d')
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
       INNER JOIN gyndata.sa_grossesse ON glb_patient.code_patient = sa_grossesse.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS NULL;
    Ce SELECT ne retourne aucun enregistrement si le champ "sa_grossesse.date_derniere_regle" est vide alors que je voudrai qu'il renvoit tous les autres champs avec une valeur nulle pour "sa_grossesse.date_derniere_regle"

  13. #13
    Rédacteur/Modérateur

    Avatar de Antoun
    Homme Profil pro
    Architecte décisionnel
    Inscrit en
    Octobre 2006
    Messages
    6 284
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Architecte décisionnel
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2006
    Messages : 6 284
    Points : 11 737
    Points
    11 737
    Par défaut
    et comme ça ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    SELECT trc_dossier.code_trace_dossier, trc_dossier.code_patient,
       CONCAT(glb_patient.nom, '^', glb_patient.prenom),
       DATE_FORMAT(glb_patient.date_naissance,'%Y%m%d') ,
       'F',
       DATE_FORMAT(trc_dossier.date_entree,'%Y%m%d'),
       'US',
       DATE_FORMAT(sa_grossesse.date_derniere_regle,'%Y%m%d')
    FROM gyndata.trc_dossier
       INNER JOIN gyndata.glb_patient ON trc_dossier.code_patient = glb_patient.code_patient
       LEFT JOIN gyndata.sa_grossesse ON glb_patient.code_patient = sa_grossesse.code_patient
    WHERE station IN ('STA_MAT_033', 'STA_MAT_019')
    AND date_entree >=CURDATE()AND date_sortie IS NULL;

  14. #14
    Candidat au Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2008
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Bon sang mais c'est bien sur !
    J'aurais du y penser.
    Ca marche impeccable. Merci.
    Bon j'attend un peu avant de cocher "Résolu"...

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 29/01/2015, 13h45
  2. Réponses: 2
    Dernier message: 28/08/2009, 13h05
  3. Problème avec une requete SQL sous access
    Par Luther13 dans le forum Requêtes et SQL.
    Réponses: 12
    Dernier message: 14/12/2005, 10h39
  4. Comment traduire une requête en XPATH ?
    Par vincent1 dans le forum XSL/XSLT/XPATH
    Réponses: 6
    Dernier message: 24/06/2005, 12h46
  5. Réponses: 7
    Dernier message: 03/06/2004, 12h46

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