Partage
  • Partager sur Facebook
  • Partager sur Twitter

requete imbriquée complexe

    3 septembre 2010 à 11:52:53

    bonjour à tous

    ma requete en elle même fonctionne, mais elle n' est pas complete, car si je selectionne certains champs cela me creer des erreurs ...

    je m'explique:

    je travaille sur plusieurs tables, mais la principale contenant les ID est celle ci :

    -- phpMyAdmin SQL Dump
    -- version 3.2.0.1
    -- http://www.phpmyadmin.net
    --
    -- Serveur: localhost
    -- Généré le : Ven 03 Septembre 2010 à 09:41
    -- Version du serveur: 5.1.36
    -- Version de PHP: 5.3.0
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    --
    -- Base de données: `sauve trade`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Structure de la table `bp_list`
    --
    
    CREATE TABLE IF NOT EXISTS `bp_list` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `ID_cat` int(10) unsigned NOT NULL,
      `nom` varchar(255) NOT NULL,
      `limited` tinyint(3) unsigned NOT NULL DEFAULT '0',
      `level` int(10) unsigned NOT NULL,
      `requis` decimal(10,1) unsigned NOT NULL,
      `acquis` tinyint(3) unsigned NOT NULL DEFAULT '0',
      `NbIng` tinyint(3) unsigned NOT NULL,
      `art01` int(10) unsigned NOT NULL DEFAULT '0',
      `qte01` int(10) unsigned NOT NULL DEFAULT '0',
      `art02` int(10) unsigned NOT NULL DEFAULT '0',
      `qte02` int(11) unsigned NOT NULL DEFAULT '0',
      `art03` int(11) unsigned NOT NULL DEFAULT '0',
      `qte03` int(11) unsigned NOT NULL DEFAULT '0',
      `art04` int(10) unsigned NOT NULL DEFAULT '0',
      `qte04` int(10) unsigned NOT NULL DEFAULT '0',
      `art05` int(10) unsigned NOT NULL DEFAULT '0',
      `qte05` int(10) unsigned NOT NULL DEFAULT '0',
      `art06` int(10) unsigned NOT NULL DEFAULT '0',
      `qte06` int(10) unsigned NOT NULL DEFAULT '0',
      `art07` int(10) unsigned NOT NULL DEFAULT '0',
      `qte07` int(10) unsigned NOT NULL DEFAULT '0',
      `art08` int(10) unsigned NOT NULL DEFAULT '0',
      `qte08` int(10) unsigned NOT NULL DEFAULT '0',
      `art09` int(10) unsigned NOT NULL DEFAULT '0',
      `qte09` int(10) unsigned NOT NULL DEFAULT '0',
      `art10` int(10) unsigned NOT NULL DEFAULT '0',
      `qte10` int(10) unsigned NOT NULL DEFAULT '0',
      `nbret` int(10) unsigned NOT NULL,
      `ret1` int(10) unsigned NOT NULL,
      `ret2` int(10) unsigned NOT NULL,
      `ret3` int(10) unsigned NOT NULL,
      `ret4` int(10) unsigned NOT NULL,
      PRIMARY KEY (`ID`),
      KEY `ID_cat` (`ID_cat`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
    
    --
    -- Contenu de la table `bp_list`
    --
    
    INSERT INTO `bp_list` (`ID`, `ID_cat`, `nom`, `limited`, `level`, `requis`, `acquis`, `NbIng`, `art01`, `qte01`, `art02`, `qte02`, `art03`, `qte03`, `art04`, `qte04`, `art05`, `qte05`, `art06`, `qte06`, `art07`, `qte07`, `art08`, `qte08`, `art09`, `qte09`, `art10`, `qte10`, `nbret`, `ret1`, `ret2`, `ret3`, `ret4`) VALUES
    (2, 1, 'Large Striped Cotton Fabric', 0, 0, '0.0', 1, 2, 85, 1, 212, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 289, 0, 0, 0),
    (8, 1, 'Fine Frote Fabric', 0, 4, '7.5', 1, 2, 79, 1, 182, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 259, 0, 0, 0),
    (9, 1, 'Lysterium', 1, 2, '2.5', 0, 2, 27, 7, 186, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 293, 0, 0, 0);
    
    --
    -- Contraintes pour les tables exportées
    --
    
    --
    -- Contraintes pour la table `bp_list`
    --
    ALTER TABLE `bp_list`
      ADD CONSTRAINT `bp_list_ibfk_1` FOREIGN KEY (`ID_cat`) REFERENCES `bp_cat` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE;
    


    il s'agit d'une table contenant des "recettes" et les champs art01 => art10 sont les ingredients nécessaires à cette recette. les champs qte01=>qte10 indiquent la quantité pour chaque ingredient.

    ma requete consiste à remplacer les ID des ingredients par leur nom (table articles).
    Voici ma requete :
    SELECT IDbp, nom, nbIng, qte01, nom1, qte02, nom2
    FROM `bp_craft`
    INNER JOIN bp_list
    ON bp_list.ID = bp_craft.IDbp
    INNER JOIN
    (
    SELECT IDart, nom_refined nom1, prix_unrefined
    FROM articles
    ) A
    ON A.IDart = bp_list.art01
    INNER JOIN
    (
    SELECT IDart, nom_refined nom2, prix_unrefined
    FROM articles
    ) B
    ON B.IDart = bp_list.art02
    


    ca fonctionne car je ne recupere que 2 ingredients, la requete complete ira chercher 10 corespondances d'ingredients, certaines recettes n'en utilisent pas 10 ... ce qui causera une erreur si l'id à chercher est "0"

    y a t il une fonction sql du style IFNULL qui n'irai pas chercher de correspondance si l'ID est 0 (ou n'existe pas) ?

    merci d'avance pour votre aide !
    • Partager sur Facebook
    • Partager sur Twitter
    Souvent la moitié de la réponse se trouve dans une question bien posée.
      3 septembre 2010 à 13:18:41

      Bonjour,

      Il y a encore mieux : les jointures externes.

      Sinon tu trouveras peut-être ton bonheur également ici.
      • Partager sur Facebook
      • Partager sur Twitter

      requete imbriquée complexe

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown