按键将两个表连接到数组中

时间:2013-12-15 14:23:56

标签: mysql arrays select join grouping

我有两个表要加入带有table2数组的数组:

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
     )
    )

)

但根据table2条目数量

,它总是倍数table1
Array
(
  [0] => Array
  (
    [value] => red
    [url] => image1
  )
  [1] => Array
  (
    [value] => red
    [url] => image2
  )
  [2] => Array
  (
    [value] => red
    [url] => image3
  )
)

我应该在查询内部更改具有所需数组的内容?

1 个答案:

答案 0 :(得分: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