Ich habe Probleme, gut formatierte Ergebnisse von meiner MySQL-Abfrage zu erhalten. HierMySQL-Abfrage - das richtige Format
ist die Datenbank:
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
Hier ist meine Frage:
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;
Hier ist das Ergebnis, das ich erhalte:
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
Hier ist das Format, das ich zu bekommen versuche:
[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
)
)
)
)
)
)
)
Kann ich das gewünschte Ergebnis aus einer Abfrage oder muss ich mehrere Anfragen tun müssen, und sie dann in PHP miteinander verbinden?
Jede Hilfe wäre willkommen!
SQL-Ergebnisse werden immer eine zweidimensionale Tabelle sein. Sie können Ihr Ergebnis nicht als Baum formatiert bekommen. Sie müssen dies mit PHP machen. Es ist jedoch nicht schwierig. – Basti
Verstanden - Danke! – tgurske