yaseen1
November 11, 2018, 7:32am
1
I have 2 separate tables states and cities.
I’ll need an SQL (Mysql) statement or PHP query to get output data in the following format. (Basically grouping the cities under their respective states).
Output example I’ll need (JSON)
"Andaman and nicobar" : ['Port Blair', 'Adilabad'],
"Tamilnadu" : ['Chennai', 'Coimbatore'],
"Maharastra" : ['Mumbai', 'Lonavala', 'Pune'],
I have the database tables as below.
SamA74
November 11, 2018, 11:26am
2
You could probably do this using PDO::FETCH_GROUP
and a JOIN
.
You need to make state_name
the first column in the query to use it as the group.
Then use json_encode()
on the result.
2 Likes
rpkamp
November 11, 2018, 12:06pm
3
<offtopic>
Ooooh, I didn’t know about that one. That’s awesome!
</offtopic>
Edit: I’m talking about FETCH_GROUP, I obviously knew joins
2 Likes
system
Closed
February 10, 2019, 7:06pm
4
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.