我無法從我的mysql查詢中獲得格式良好的結果。MySQL查詢 - 獲取正確的格式
這裏是數據庫:
customers table
id name
1 Tim
2 Lauren
3 Prada
tanks table
id c_id location
1 1 Living Room
2 2 Living Room
3 2 Kitchen
4 3 Bedroom
5 3 Hallway
fish table
id t_id color
1 1 red
2 1 blue
3 2 white
4 2 green
5 2 black
6 3 red
7 3 orange
8 4 white
9 4 blue
10 5 black
這裏是我的查詢:
SELECT * FROM customers
LEFT JOIN tanks ON (tanks.c_id = customers.id)
LEFT JOIN fish ON (fish.t_id = tanks.id)
GROUP BY customers.id, tanks.id;
這裏是我得到的結果是:
id name id c_id location id t_id color
1 Tim 1 1 Living Room 1 1 red
2 Lauren 2 2 Living Room 3 2 white
2 Lauren 3 2 Kitchen 6 3 red
3 Prada 4 3 Bedroom 8 4 white
3 Prada 5 3 Hallway 10 5 black
這裏是我想獲得的格式爲:
[0] => Array
(
[customers] => Array
(
[0] => Array
(
[id] => 2
[title] => Lauren
[tanks] => Array
(
[0] => Array
(
[id] => 2
[location] => Living Room
[fish] => Array
(
[0] => Array
(
[id] => 3
[color] => white
)
[1] => Array
(
[id] => 4
[color] => green
)
[2] => Array
(
[id] => 5
[color] => black
)
)
)
[1] => Array
(
[id] => 3
[location] => Kitchen
[fish] => Array
(
[0] => Array
(
[id] => 6
[color] => red
)
[1] => Array
(
[id] => 7
[color] => orange
)
)
)
)
)
)
)
我能得到期望的結果從一個查詢或做我需要做多個查詢,然後加入他們一起在PHP?
任何幫助,將不勝感激!
SQL的結果將始終是一個二維表。你不能把你的結果格式化爲樹。你將不得不使用PHP來做到這一點。這並不難。 – Basti 2012-03-25 22:17:57
明白了 - 謝謝! – tgurske 2012-03-26 01:09:30