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 :

Aide pour faire les jointures sur une requête


Sujet :

Requêtes MySQL

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut Aide pour faire les jointures sur une requête
    Bonjour,

    J'ai cette requête :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    SELECT exemplaires.expl_cb AS 'Code-barre',expl_cote AS 'Cote',tit1 AS 'Titre',CONCAT(author_name,' ',author_rejete) AS 'Auteur',CONCAT(empr_nom,' ',empr_prenom) AS 'Emprunteur',empr_tel1 AS 'Téléphone',empr_mail AS 'Courriel',pret_date AS 'Sortie',pret_retour AS 'Retour'
    FROM pret,empr,notices,responsability,authors,exemplaires
    WHERE (pret_retour BETWEEN !!Date_min!! AND !!Date_max!!) AND pret_idempr=id_empr AND pret_idexpl=expl_id AND expl_notice=notice_id AND responsability_notice=expl_notice AND responsability_author=author_id
    Mais le problème est que si il n'y a pas d'auteur la requête ne renvoi rien...
    Car un auteur (table authors) est lié à une notice (table notices) via la table responsability (qui associe à un id_author un id_notice)...

    Du coup je ne peux pas faire les jointures entre mes tables de la manière ci-dessus, je dois utiliser les OUTER JOIN...

    Mais je n'arrive pas à modifier ma requête...

    J'ai essayé ca :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    SELECT exemplaires.expl_cb AS 'Code-barre',expl_cote AS 'Cote',tit1 AS 'Titre',CONCAT(author_name,' ',author_rejete) AS 'Auteur',CONCAT(empr_nom,' ',empr_prenom) AS 'Emprunteur',empr_tel1 AS 'Téléphone',empr_mail AS 'Courriel',pret_date AS 'Sortie',pret_retour AS 'Retour' 
    FROM pret LEFT OUTER JOIN empr ON pret_idempr=id_empr LEFT OUTER JOIN notices ON pret_idexpl=expl_id LEFT OUTER JOIN exemplaires ON expl_notice=notice_id LEFT OUTER JOIN responsability ON responsability_notice=expl_notice LEFT OUTER JOIN authors ON responsability_author=author_id 
    WHERE (pret_retour BETWEEN !!Date_min!! AND !!Date_max!!)
    Mais j'ai l'erreur suivante :
    #1054 - Champ 'expl_id' inconnu dans on clause
    Mon problème est que toutes mes tables ne sont pas liées à une et une seule table... Du coup je ne sais pas trop comment m'y prendre...

    Est-ce que quelqu'un pourrait me donner un coup de main svp ?

  2. #2
    Modérateur

    Avatar de CinePhil
    Homme Profil pro
    Ingénieur d'études en informatique
    Inscrit en
    Août 2006
    Messages
    16 801
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    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 : 16 801
    Points : 34 063
    Points
    34 063
    Billets dans le blog
    14
    Par défaut
    Tel que tu as construit ta requête :
    1 pret peut avoir plusieurs emprunteurs. Bizarre non ?
    1 pret peut avoir plusieurs notices.
    1 notice peut avoir plusieurs exemplaires.
    1 exemplaire peut avoir plusieurs responsibility.
    1 responsability peut avoir plusieurs authors.

    A toi de nous donner les bonnes règles de gestion et nous pourrons mieux t'aider à construire ta requête.

    Quant au message d'erreur, il semble en effet bizarre qu'une jointure entre les tables pret et notices aille chercher la colonne (et pas champ) expl_id qui semble plutôt l'identifiant de la table exemplaires !

    En nous donnant la structure des tables, ce sera également plus facile pour nous de t'aider.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut
    Je me doute que j'ai mal construit ma requête...

    Voici les règles :
    1 pret ne peut avoir qu'un emprunteur.
    1 pret est associé à une notice.
    1 notice peut avoir plusieurs exemplaires.
    1 notice peut avoir plusieurs authors (via la table responsability)
    Voici la structure des tables :
    TABLE pret (
    pret_idempr int(10) unsigned NOT NULL DEFAULT '0',
    pret_idexpl int(10) unsigned NOT NULL DEFAULT '0',
    pret_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    pret_retour date DEFAULT NULL,
    pret_arc_id int(10) unsigned NOT NULL DEFAULT '0',
    niveau_relance int(1) NOT NULL DEFAULT '0',
    date_relance date DEFAULT '0000-00-00',
    printed int(1) NOT NULL DEFAULT '0',
    retour_initial date DEFAULT '0000-00-00',
    cpt_prolongation int(1) NOT NULL DEFAULT '0',
    pret_temp varchar(50) NOT NULL DEFAULT '',
    PRIMARY KEY (pret_idexpl),
    KEY i_pret_idempr (pret_idempr)
    )

    TABLE empr (
    id_empr int(10) unsigned NOT NULL AUTO_INCREMENT,
    empr_cb varchar(255) DEFAULT NULL,
    empr_nom varchar(255) NOT NULL DEFAULT '',
    empr_prenom varchar(255) NOT NULL DEFAULT '',
    empr_adr1 varchar(255) NOT NULL DEFAULT '',
    empr_adr2 varchar(255) NOT NULL DEFAULT '',
    empr_cp varchar(10) NOT NULL DEFAULT '',
    empr_ville varchar(255) NOT NULL DEFAULT '',
    empr_pays varchar(255) NOT NULL DEFAULT '',
    empr_mail varchar(255) NOT NULL DEFAULT '',
    empr_tel1 varchar(255) NOT NULL DEFAULT '',
    empr_tel2 varchar(255) NOT NULL DEFAULT '',
    empr_prof varchar(255) NOT NULL DEFAULT '',
    empr_year int(4) unsigned NOT NULL DEFAULT '0',
    empr_categ smallint(5) unsigned NOT NULL DEFAULT '0',
    empr_codestat smallint(5) unsigned NOT NULL DEFAULT '0',
    empr_creation datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    empr_modif date NOT NULL DEFAULT '0000-00-00',
    empr_sexe tinyint(3) unsigned NOT NULL DEFAULT '0',
    empr_login varchar(255) NOT NULL DEFAULT '',
    empr_password varchar(255) NOT NULL DEFAULT '',
    empr_date_adhesion date DEFAULT NULL,
    empr_date_expiration date DEFAULT NULL,
    empr_msg tinytext,
    empr_lang varchar(10) NOT NULL DEFAULT 'fr_FR',
    empr_ldap tinyint(1) unsigned DEFAULT '0',
    type_abt int(1) NOT NULL DEFAULT '0',
    last_loan_date date DEFAULT NULL,
    empr_location int(6) unsigned NOT NULL DEFAULT '1',
    date_fin_blocage date NOT NULL DEFAULT '0000-00-00',
    total_loans bigint(20) unsigned NOT NULL DEFAULT '0',
    empr_statut bigint(20) unsigned NOT NULL DEFAULT '1',
    cle_validation varchar(255) NOT NULL DEFAULT '',
    PRIMARY KEY (id_empr),
    UNIQUE KEY empr_cb (empr_cb),
    KEY empr_nom (empr_nom),
    KEY empr_date_adhesion (empr_date_adhesion),
    KEY empr_date_expiration (empr_date_expiration),
    KEY i_empr_categ (empr_categ),
    KEY i_empr_codestat (empr_codestat),
    KEY i_empr_location (empr_location),
    KEY i_empr_statut (empr_statut),
    KEY i_empr_typabt (type_abt)
    )

    TABLE notices (
    notice_id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
    typdoc char(2) NOT NULL DEFAULT 'a',
    tit1 tinytext NOT NULL,
    tit2 tinytext NOT NULL,
    tit3 tinytext NOT NULL,
    tit4 tinytext NOT NULL,
    tparent_id mediumint(8) unsigned NOT NULL DEFAULT '0',
    tnvol varchar(100) NOT NULL DEFAULT '',
    ed1_id mediumint(8) unsigned NOT NULL DEFAULT '0',
    ed2_id mediumint(8) unsigned NOT NULL DEFAULT '0',
    coll_id mediumint(8) unsigned NOT NULL DEFAULT '0',
    subcoll_id mediumint(8) unsigned NOT NULL DEFAULT '0',
    `year` varchar(50) DEFAULT NULL,
    nocoll varchar(255) DEFAULT NULL,
    mention_edition varchar(255) NOT NULL DEFAULT '',
    `code` varchar(50) NOT NULL DEFAULT '',
    npages varchar(255) DEFAULT NULL,
    ill varchar(255) DEFAULT NULL,
    size varchar(255) DEFAULT NULL,
    accomp varchar(255) DEFAULT NULL,
    n_gen text NOT NULL,
    n_contenu text NOT NULL,
    n_resume text NOT NULL,
    lien text NOT NULL,
    eformat varchar(255) NOT NULL DEFAULT '',
    index_l text NOT NULL,
    indexint int(8) unsigned NOT NULL DEFAULT '0',
    index_serie tinytext,
    index_matieres text NOT NULL,
    niveau_biblio char(1) NOT NULL DEFAULT 'm',
    niveau_hierar char(1) NOT NULL DEFAULT '0',
    origine_catalogage int(8) unsigned NOT NULL DEFAULT '1',
    prix varchar(255) NOT NULL DEFAULT '',
    index_n_gen text,
    index_n_contenu text,
    index_n_resume text,
    index_sew text,
    index_wew text,
    statut int(5) NOT NULL DEFAULT '1',
    commentaire_gestion text NOT NULL,
    create_date datetime NOT NULL DEFAULT '2005-01-01 00:00:00',
    update_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    signature varchar(255) NOT NULL DEFAULT '',
    thumbnail_url mediumblob NOT NULL,
    date_parution date NOT NULL DEFAULT '0000-00-00',
    opac_visible_bulletinage tinyint(3) unsigned NOT NULL DEFAULT '1',
    PRIMARY KEY (notice_id),
    KEY typdoc (typdoc),
    KEY tparent_id (tparent_id),
    KEY ed1_id (ed1_id),
    KEY ed2_id (ed2_id),
    KEY coll_id (coll_id),
    KEY subcoll_id (subcoll_id),
    KEY cb (`code`),
    KEY indexint (indexint),
    KEY sig_index (signature),
    KEY i_notice_n_biblio (niveau_biblio),
    KEY i_notice_n_hierar (niveau_hierar),
    KEY notice_eformat (eformat),
    KEY i_date_parution (date_parution)
    )

    TABLE responsability (
    responsability_author mediumint(8) unsigned NOT NULL DEFAULT '0',
    responsability_notice mediumint(8) unsigned NOT NULL DEFAULT '0',
    responsability_fonction varchar(4) NOT NULL DEFAULT '',
    responsability_type mediumint(1) unsigned NOT NULL DEFAULT '0',
    responsability_ordre smallint(2) unsigned NOT NULL DEFAULT '0',
    PRIMARY KEY (responsability_author,responsability_notice,responsability_fonction),
    KEY responsability_notice (responsability_notice)
    )

    TABLE `authors` (
    author_id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
    author_type enum('70','71','72') NOT NULL DEFAULT '70',
    author_name varchar(255) NOT NULL DEFAULT '',
    author_rejete varchar(255) NOT NULL DEFAULT '',
    author_date varchar(255) NOT NULL DEFAULT '',
    author_see mediumint(8) unsigned NOT NULL DEFAULT '0',
    author_web varchar(255) NOT NULL DEFAULT '',
    index_author text,
    author_comment text,
    author_lieu varchar(255) NOT NULL DEFAULT '',
    author_ville varchar(255) NOT NULL DEFAULT '',
    author_pays varchar(255) NOT NULL DEFAULT '',
    author_subdivision varchar(255) NOT NULL DEFAULT '',
    author_numero varchar(50) NOT NULL DEFAULT '',
    PRIMARY KEY (author_id),
    KEY author_see (author_see),
    KEY author_name (author_name),
    KEY author_rejete (author_rejete)
    )

    TABLE exemplaires (
    expl_id int(10) unsigned NOT NULL AUTO_INCREMENT,
    expl_cb varchar(50) NOT NULL DEFAULT '',
    expl_notice int(10) unsigned NOT NULL DEFAULT '0',
    expl_bulletin int(10) unsigned NOT NULL DEFAULT '0',
    expl_typdoc int(5) unsigned NOT NULL DEFAULT '0',
    expl_cote varchar(50) NOT NULL DEFAULT '',
    expl_section smallint(5) unsigned NOT NULL DEFAULT '0',
    expl_statut smallint(5) unsigned NOT NULL DEFAULT '0',
    expl_location smallint(5) unsigned NOT NULL DEFAULT '0',
    expl_codestat smallint(5) unsigned NOT NULL DEFAULT '0',
    expl_date_depot date NOT NULL DEFAULT '0000-00-00',
    expl_date_retour date NOT NULL DEFAULT '0000-00-00',
    expl_note tinytext NOT NULL,
    expl_prix varchar(255) NOT NULL DEFAULT '',
    expl_owner mediumint(8) unsigned NOT NULL DEFAULT '0',
    expl_lastempr int(10) unsigned NOT NULL DEFAULT '0',
    last_loan_date date DEFAULT NULL,
    create_date datetime NOT NULL DEFAULT '2005-01-01 00:00:00',
    update_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    type_antivol int(1) unsigned NOT NULL DEFAULT '0',
    transfert_location_origine smallint(5) unsigned NOT NULL DEFAULT '0',
    transfert_statut_origine smallint(5) unsigned NOT NULL DEFAULT '0',
    expl_comment varchar(255) NOT NULL DEFAULT '',
    PRIMARY KEY (expl_id),
    UNIQUE KEY expl_cb (expl_cb),
    KEY expl_typdoc (expl_typdoc),
    KEY expl_cote (expl_cote),
    KEY expl_notice (expl_notice),
    KEY expl_codestat (expl_codestat),
    KEY expl_owner (expl_owner),
    KEY expl_bulletin (expl_bulletin),
    KEY i_expl_location (expl_location),
    KEY i_expl_section (expl_section),
    KEY i_expl_statut (expl_statut),
    KEY i_expl_lastempr (expl_lastempr)
    )
    Merci pour votre aide

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut
    Personne ?
    Please help !

    J'ai juste besoin de savoir comment lier mes tables de manière à ce que si il n'y ait pas d'auteur associé la requête me renvoi quand même un résultat avec un auteur vide (NULL)...

    En gros si il n'y a pas d'association entre 1 auteur et une notice via la table responsability (les champs responsability_author et responsability_notice) la requête doit quand même retourner la notice avec NULL dans le champ author_name...

Discussions similaires

  1. [AC-2003] Faire SOMME.SI sur une requête pour produire un etat
    Par christophesav dans le forum IHM
    Réponses: 2
    Dernier message: 12/12/2011, 01h49
  2. Réponses: 4
    Dernier message: 20/06/2011, 02h01
  3. cherche aide pour améliorer le traitement d'une requête
    Par nomade333 dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 22/04/2008, 14h15
  4. Besoin d'aide pour bloquer un nombre sur une TextBox
    Par Torrent74 dans le forum Flash
    Réponses: 2
    Dernier message: 24/10/2007, 20h14
  5. Réponses: 8
    Dernier message: 21/09/2007, 14h51

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