First of all, i know my question heading is little bit or more confusing so let me try to explain.
I have 3 tables,
- Companies
- company_branches
- cities
so with those tables, i want to select all from companies and join company_branches then join cities.
All WITH JUST ONE SQL QUERY, and i want to end up with a list of companies, and each company having it's branches (array) under a column called branches.
See examples bellow.
companies_table
id | name
--------+-----------------------
1 | microsoft
--------+-----------------------
2 | facebook
company_branches_table
id | company_id | city_id
--------+-----------------+-------------
1 | 1 | 3
--------+-----------------+-------------
2 | 1 | 2
--------+-----------------+-------------
3 | 2 | 1
cities
id | name
--------+-------------
1 | LA
--------+-------------
2 | New york
--------+-------------
3 | Chicago
Here is how i wanted my results to look like
in Json or associative array
[
{
id : 1,
name : microsoft,
branches : [
{
id : 2,
city_name : New york
},
{
id : 3,
city_name : Chicago
}
]
},
{
id : 2,
name : facebook,
branches : [
{
id : 1,
city_name : LA
}
]
}
]
I hope u'll understand. thanks in advance
[Edit]
Listed tables at the top