2016-03-31 68 views
0

如何加入一些表,然後生成一個數組。MySQL JOIN ARRAY

表「水果」是主表。

表格「有序」中的「fruit_id」字段取自表格「fruits」ID。

表:水果

------------------------------------------------------+ 
id   fruits   date_created 
------------------------------------------------------+ 
1   Apple   2016-03-31 14:29:29 
2   Blueberry  2016-03-30 14:22:54 
3   Coconut   2016-03-30 14:19:12 
------------------------------------------------------+ 

表:責令

------------------------------------------------------------------------------------------------+ 
id   fruit_id   package_id  price_with_ship   price_without_ship 
------------------------------------------------------------------------------------------------+ 
1   3     10    150      0 
2   3     11    0      110 
3   2     10    0      87 
4   2     11    0      95 
5   2     12    100      0 
6   1     12    75      0 
------------------------------------------------------------------------------------------------+ 

這裏有結果,我建議。

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => Apple 
      [date_created] => 2016-03-31 14:29:29 
      [package_id] => Array 
            (
             [0] => 10 
             [1] => 11 
            ) 
      [price_with_ship] => Array 
            (
             [0] => 150 
             [1] => 0 
            ) 
      [price_with_ship] => Array 
            (
             [0] => 0 
             [1] => 110 
            ) 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => Blueberry 
      [date_created] => 2016-03-30 14:22:54 
      [package_id] => Array 
            (
             [0] => 10 
             [1] => 11 
             [2] => 12 
            ) 
      [price_with_ship] => Array 
            (
             [0] => 0 
             [1] => 0 
             [2] => 100 
            ) 
      [price_with_ship] => Array 
            (
             [0] => 87 
             [1] => 95 
             [2] => 0 
            ) 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [name] => Coconut 
      [date_created] => 2016-03-30 14:19:12 
      [package_id] => 12 
      [price_with_ship] => 75 
      [price_with_ship] => 0 
     ) 

感謝您的高級幫助。

+0

你需要什麼樣的輸出。 –

+0

請澄清你的問題。 您是否期待輸出結果爲每個水果的訂單詳細信息? – Stuart

+0

@Stuart是的,你是對的。 – cocksparrer

回答

0

按如下─

select f.*, o.* 
from fruit as f 
join order as o on o.fruit_id=f.id 
where f.id=1; 
+0

謝謝。這些取決於表「排序」行,而不取決於「水果」表。 – cocksparrer

+0

沒有得到你想說的......這只是一個加入的例子......如果你分享你的需求/輸出,那麼我可以幫你。 –

0

您需要2個查詢

  1. 得到id, name, date_created你可以加入:

    Select o.id, f.name, f.date_created From ordered o left join fruits f ON o.fruit_id = f.id

  2. 每個記錄選擇的細節:

    Select package_id, price_with_ship, price_without_ship From ordered Where id = ? -- replace with id from the loop