2015-06-27 65 views
0

我有兩個表:訂單和product_orders 訂單表中有一個id。即:9 和product_orders中的order_id(作爲外鍵)重複3次。所以我希望product_orders表中的這三個條目在orders表中有一個條目。 我的查詢是:按左數據組加入

SELECT orders.*,order_products.* FROM orders LEFT JOIN order_products ON orders.id=order_products.order_id 

這給:

Array 
(
    [0] => Array 
     (
      [orders] => Array 
       (
        [id] => 9 
        [name] => Abdus Sattar Bhuiyan 
        [email] => [email protected] 
        [mobile] => 01673050495 
        [alt_mobile] => 01818953250 
        [city_id] => 2 
        [location_id] => 5 
        [status] => No contact 
        [chashed] => NO 
        [created] => 2015-06-27 12:49:34 
        [modified] => 2015-06-27 12:49:34 
        [comment] => 
       ) 

      [order_products] => Array 
       (
        [id] => 2 
        [order_id] => 9 
        [product_id] => 1 
        [pieces] => 1 
       ) 

     ) 

    [1] => Array 
     (
      [orders] => Array 
       (
        [id] => 9 
        [name] => Abdus Sattar Bhuiyan 
        [email] => [email protected] 
        [mobile] => 01673050495 
        [alt_mobile] => 01818953250 
        [city_id] => 2 
        [location_id] => 5 
        [status] => No contact 
        [chashed] => NO 
        [created] => 2015-06-27 12:49:34 
        [modified] => 2015-06-27 12:49:34 
        [comment] => 
       ) 

      [order_products] => Array 
       (
        [id] => 3 
        [order_id] => 9 
        [product_id] => 2 
        [pieces] => 1 
       ) 

     ) 

    [2] => Array 
     (
      [orders] => Array 
       (
        [id] => 9 
        [name] => Abdus Sattar Bhuiyan 
        [email] => [email protected] 
        [mobile] => 01673050495 
        [alt_mobile] => 01818953250 
        [city_id] => 2 
        [location_id] => 5 
        [status] => No contact 
        [chashed] => NO 
        [created] => 2015-06-27 12:49:34 
        [modified] => 2015-06-27 12:49:34 
        [comment] => 
       ) 

      [order_products] => Array 
       (
        [id] => 4 
        [order_id] => 9 
        [product_id] => 3 
        [pieces] => 1 
       ) 

     ) 

) 

我預期的結果是:

Array 
    (
     [0] => Array 
      (
       [orders] => Array 
        (
         [id] => 9 
         [name] => Abdus Sattar Bhuiyan 
         [email] => [email protected] 
         [mobile] => 01673050495 
         [alt_mobile] => 01818953250 
         [city_id] => 2 
         [location_id] => 5 
         [status] => No contact 
         [chashed] => NO 
         [created] => 2015-06-27 12:49:34 
         [modified] => 2015-06-27 12:49:34 
         [comment] => 
        ) 

       [order_products] => Array 
        (
          [0] => Array(
            [id] => 2 
            [order_id] => 9 
            [product_id] => 1 
            [pieces] => 1 
           ) 
          [1] => Array(
            [id] => 3 
            [order_id] => 9 
            [product_id] => 2 
            [pieces] => 1 
           ) 
          [2] => Array(
            [id] => 4 
            [order_id] => 9 
            [product_id] => 3 
            [pieces] => 1 
           ) 

        ) 

      ) 
    ) 

    I used GROUP BY orders.id. But no luck. 
+0

您正在使用哪些DBMS?和哪種編程語言來獲得您的查詢結果? – Renzo

+0

mysql數據庫和我正在使用php語言 –

+0

您可以使用'array_column()'來聚合它們。看到我的答案在這裏你的相關問題:http://stackoverflow.com/questions/31093087/formatting-an-array-using-php/31094106#31094106 –

回答

2

你基本上期待的結果,是不是能夠獲得一個SQL查詢:你想要一個等級結構,其中一個order和一組order_product爲那個order,而查詢返回一個平面表,每個order連同一行,連同不同order_product的信息。這是SQL工作的方式。

解決您的問題的一種方法是在獲取所有行後,對php進行一種後處理,將所有這些部分組合在一起,然後生成一個新的數組,用於與order_product部分相關的部分該訂單。

+1

在這裏重要的一點:這不是MySQL *支持的功能。大多數數據庫都支持分層和遞歸查詢。 –