2013-12-15 51 views
1

我有想加入到數組與數組表2兩個表:連接兩個表到數組的關鍵

table1 
id value 
---------- 
1 red 
2 blue 

table2 
id url 
----------- 
1 image1 
1 image2 
1 image3 
2 image1 
2 image2 
2 image3 

MySQL查詢是:

SELECT HIGH_PRIORITY * FROM table1 
        LEFT JOIN table2 ON table1.id = table2.id 
         WHERE table1.id = 1 

我期待有如下結果:

Array 
(
    [value] => red 
    (
    [url] => Array 
    (
     [0] => image1 
     [1] => image2 
     [2] => image3 
    ) 
    ) 

) 

但它始終是根據表2的倍數table1的條目數量

Array 
(
    [0] => Array 
    (
    [value] => red 
    [url] => image1 
) 
    [1] => Array 
    (
    [value] => red 
    [url] => image2 
) 
    [2] => Array 
    (
    [value] => red 
    [url] => image3 
) 
) 

我應該查詢裏面什麼改變成期望的陣列?

+0

你可以做一個GROUP_CONCAT和you'd有你的第一陣列,但與主要0 =「圖像1,圖像2,圖像3」 – Mihai

+0

你爲什麼要使用的MyISAM而不是InnoDB的? – Strawberry

回答

0

看起來運營商的關聯性與您的期望相反。您是否嘗試過這樣的:

SELECT HIGH_PRIORITY * FROM (
    SELECT table1.id, table2.url 
    FROM table1 
    LEFT JOIN table2 ON table1.id = table2.id 
    WHERE table1.id = 1 
) data