2014-09-29 49 views
-3

我正在處理多個連接...而且我無法完成連接。多個連接6 itens

這裏談到的問題

CREATE TABLE IF NOT EXISTS `catleathers` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `code` varchar(55) NOT NULL, 
    `name` varchar(55) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `id` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ; 

CREATE TABLE IF NOT EXISTS `catsoles` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `code` varchar(55) NOT NULL, 
    `name` varchar(55) NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `id` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ; 

CREATE TABLE IF NOT EXISTS `products` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `code` varchar(50) NOT NULL, 
    `name` char(255) NOT NULL, 
    `unit` varchar(255) DEFAULT '1', 
    `size` varchar(255) DEFAULT '1', 
    `cost` decimal(25,2) DEFAULT NULL, 
    `price` decimal(25,2) NOT NULL, 
    `alert_quantity` int(11) NOT NULL DEFAULT '20', 
    `image` varchar(255) DEFAULT 'no_image.jpg', 
    `category_id` int(11) NOT NULL, 
    `subcategory_id` int(11) DEFAULT NULL, 
    `catleather_id_a` int(11) NOT NULL, 
    `subcatleather_id_a` int(11) NOT NULL, 
    `catleather_id_b` int(11) NOT NULL, 
    `subcatleather_id_b` int(11) NOT NULL, 
    `catleather_id_c` int(11) NOT NULL, 
    `subcatleather_id_c` int(11) NOT NULL, 
    `catsole_id_a` int(11) NOT NULL, 
    `subcatsole_id_a` int(11) NOT NULL, 
    `catsole_id_b` int(11) NOT NULL, 
    `subcatsole_id_b` int(11) NOT NULL, 
    `catsole_id_c` int(11) NOT NULL, 
    `subcatsole_id_c` int(11) NOT NULL, 
    `sortido` varchar(100) NOT NULL, 
    `lining` varchar(150) NOT NULL, 
    `lining_description` varchar(255) NOT NULL, 
    `sole_a` varchar(255) DEFAULT NULL, 
    `sole_b` varchar(255) DEFAULT NULL, 
    `sole_a_description` varchar(255) DEFAULT NULL, 
    `sole_b_description` varchar(255) DEFAULT NULL, 
    `leather_a` varchar(255) DEFAULT NULL, 
    `leather_b` varchar(255) DEFAULT NULL, 
    `leather_c` varchar(255) DEFAULT NULL, 
    `leather_a_description` varchar(255) DEFAULT NULL, 
    `leather_b_description` varchar(255) DEFAULT NULL, 
    `leather_c_description` varchar(255) DEFAULT NULL, 
    `carts` varchar(100) NOT NULL, 
    `quantity` int(11) DEFAULT NULL, 
    `tax_rate` int(11) DEFAULT NULL, 
    `track_quantity` tinyint(4) DEFAULT '1', 
    `details` varchar(1000) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `code` (`code`), 
    KEY `category_id` (`category_id`), 
    KEY `id` (`id`), 
    KEY `id_2` (`id`), 
    KEY `category_id_2` (`category_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=79 

我怎樣才能做到這一點多次join?

 ->join('categories', 'products.category_id=categories.id', 'left') 
     ->join('subcategories', 'products.subcategory_id=subcategories.id', 'left') 


     ->join('catleathers', 'products.catleather_id_a=catleathers.id', 'left') 
     ->join('subcatleathers', 'products.subcatleather_id_a=subcatleathers.id', 'left') 

     ->join('catleathers', 'products.catleather_id_b=catleathers.id', 'left') 
     ->join('subcatleathers', 'products.subcatleather_id_b=subcatleathers.id', 'left') 

     ->join('catleathers', 'products.catleather_id_c=catleathers.id', 'left') 
     ->join('subcatleathers', 'products.subcatleather_id_c=subcatleathers.id', 'left') 

     ->join('catsoles', 'products.catsole_id_a=catsoles.id', 'left') 
     ->join('subcatsoles', 'products.subcatsole_id_a=subcatsoles.id', 'left') 

     ->join('catsoles', 'products.catsole_id_b=catsoles.id', 'left') 
     ->join('subcatsoles', 'products.subcatsole_id_b=subcatsoles.id', 'left') 

     ->join('catsoles', 'products.catsole_id_c=catsoles.id', 'left') 
     ->join('subcatsoles', 'products.subcatsole_id_c=subcatsoles.id', 'left') 

     ->join('tbl_lining', 'products.lining=tbl_lining.id', 'left') 

     ->group_by("products.id"); 

有人可以給我這個倍數連接的手?

+0

你能提供一個模式(表定義)和你想​​要得到的輸出的例子嗎? – StingyJack 2014-09-29 17:24:43

+0

當我說表定義,我的意思是這樣的... http://www.w3schools.com/sql/sql_create_table.asp。你的問題是定義結果集的列列表,而不是表格。 – StingyJack 2014-09-29 17:26:25

+0

謝謝 這裏有雲 我需要 – gatuso 2014-09-29 18:22:15

回答

0

這應該是你所需要的,通告的「A」,「B」和「C」的情況下,我抓住了alias.Column AS FinalColumnName的查詢。

SELECT 
     p.id, 
     p.name, 
     p.code, 
     clA.code as claCode, 
     sclA.code as sclaCode, 
     clB.code as clbCode, 
     sclB.code as sclbCode, 
     clC.code as clcCode, 
     sclC.code as sclcCode 
    FROM 
     products p 
     INNER JOIN catleathers clA 
      ON p.catleather_id_a = clA.id 
     INNER JOIN subcatleathers sclA 
      ON p.subcatleather_id_a = sclA.id 
     INNER JOIN catleathers clB 
      ON p.catleather_id_b = clB.id 
     INNER JOIN subcatleathers sclB 
      ON p.subcatleather_id_b = sclB.id 
     INNER JOIN catleathers clC 
      ON p.catleather_id_c = clC.id 
     INNER JOIN subcatleathers sclC 
      ON p.subcatleather_id_c = sclC.id 

但是,我也看到,一些引擎不喜歡的「AS」的條款,所以有問題的列可能需要進行調整,以

 clA.code claCode, 
     sclA.code sclaCode, 
     clB.code clbCode, 
     sclB.code sclbCode, 
     clC.code clcCode, 
     sclC.code sclcCode 
0

我冒昧給你的問題添加表格模式,我想我明白你在找什麼。我認爲這應該足以說明您可以填寫其餘表格的起點。

SELECT 
    p.id 
    , p.name 
    , p.code 
    , 'claCode' = clA.code 
    , 'claName' = clA.name 
    , 'clbCode' = clB.code 
    , 'clbName' = clB.name 
FROM Products p 
    INNER JOIN catleathers clA 
    ON p.catleather_id_a = clA.id 
    INNER JOIN catleathers clB 
    ON p.catleather_id_b = clB.id 
0

通過DRapp

SELECT 
     p.id, 
     p.name, 
     p.code, 
     clA.code as claCode, 
     sclA.code as sclaCode, 
     clB.code as clbCode, 
     sclB.code as sclbCode, 
     clC.code as clcCode, 
     sclC.code as sclcCode, 

    catSoleA.code as catSoleACode, 
    subcatSoleA.code as subcatSoleACode, 

    catSoleB.code as catSoleBCode, 
    subcatSoleB.code as subcatSoleBCode, 

    catSoleC.code as catSoleCCode, 
    subcatSoleC.code as subcatSoleCCode 

    FROM 
     products p 
     INNER JOIN catleathers clA 
      ON p.catleather_id_a = clA.id 
     INNER JOIN subcatleathers sclA 
      ON p.subcatleather_id_a = sclA.id 
     INNER JOIN catleathers clB 
      ON p.catleather_id_b = clB.id 
     INNER JOIN subcatleathers sclB 
      ON p.subcatleather_id_b = sclB.id 
     INNER JOIN catleathers clC 
      ON p.catleather_id_c = clC.id 
     INNER JOIN subcatleathers sclC 
      ON p.subcatleather_id_c = sclC.id 

     INNER JOIN catsoles catSoleA 
      ON p.catsole_id_a = catSoleA.id 
     INNER JOIN subcatsoles subcatSoleA 
      ON p.subcatsole_id_a = subcatSoleA.id 

     INNER JOIN catsoles catSoleB 
      ON p.catsole_id_b = catSoleB.id 
     INNER JOIN subcatsoles subcatSoleB 
      ON p.subcatsole_id_b = subcatSoleB.id 

     INNER JOIN catsoles catSoleC 
      ON p.catsole_id_c = catSoleC.id 
     INNER JOIN subcatsoles subcatSoleC 
      ON p.subcatsole_id_c = subcatSoleC.id 
解決

它適用於SQL查詢,但不工作的PHP編碼

->select(" 
       products.id as productid, 
       products.image as image, 
       products.code as code, 
       pproducts.name as name, 

       categories.name as cname, 
       subcategories.name as subcategory, 
       products.cost, 
       products.price, 


       clA.code as claCode, 
       sclA.code as sclaCode, 
       clB.code as clbCode, 
       sclB.code as sclbCode, 
       clC.code as clcCode, 
       sclC.code as sclcCode, 

       catSoleA.code as catSoleACode, 
       subcatSoleA.code as subcatSoleACode, 

       catSoleB.code as catSoleBCode, 
       subcatSoleB.code as subcatSoleBCode, 

       catSoleC.code as catSoleCCode, 
       subcatSoleC.code as subcatSoleCCode 




      products.details", FALSE) 

      ->from('products') 


      ->join('catleathers clA', 'products.catleather_id_a = clA.id', 'INNER') 
      ->join('subcatleathers sclA', 'products.subcatleather_id_a = sclA.id', 'INNER') 

      ->join('catleathers clB', 'products.catleather_id_a = clB.id', 'INNER') 
      ->join('subcatleathers sclB', 'products.subcatleather_id_a = sclB.id', 'INNER') 

      ->join('catleathers clC', 'products.catleather_id_a = clC.id', 'INNER') 
      ->join('subcatleathers sclC', 'products.subcatleather_id_a = sclC.id', 'INNER') 

      ->join('catsoles catSoleA', 'products.catsole_id_a = catSoleA.id', 'INNER') 
      ->join('subcatsoles subcatSoleA', 'products.subcatsole_id_a = subcatSoleA.id', 'INNER') 

      ->join('catsoles catSoleB', 'products.catsole_id_b = catSoleB.id', 'INNER') 
      ->join('subcatsoles subcatSoleB', 'products.subcatsole_id_b = subcatSoleB.id', 'INNER') 

      ->join('catsoles catSoleC', 'products.catsole_id_c = catSoleC.id', 'INNER') 
      ->join('subcatsoles subcatSoleC', 'products.subcatsole_id_c = subcatSoleC.id', 'INNER') 




      ->join('tbl_lining', 'products.lining=tbl_lining.id', 'left') 






      ->group_by("products.id"); 

我在做什麼錯了?