Reputation: 5557
I have to join multiple tables, some with one to many mappings, and convert every entry in the joined table to a single row entry in a csv file using PHP.
I know I am basically going from a 3D to 2D representation, but it is a formatting requirement for the csv file.
The MySQL structure consists of tables "questions", "people" and "trips". "people" table is linked to "questions" table by the id of "questions". Also in the same way "trips" is linked to "people" by the id of "people". There are one to many relationship between both "questions" to "people" and "people" to "trips"
The idea is to have a csv file with one "question" entry per row. This row entry will be the joined result of all 3 tables. The number of people and trips per questions entry is not consistent, therefore spaces are required if there are fewer people in some "questions" entries than other. The same with the trips.
What is the best way to get this done in PHP, except for brute force coding this with many queries, for loops etc. I am trying to get the MySQL query going, but cannot wrap my head around this problem.
Upvotes: 1
Views: 375
Reputation: 1412
I think you're looking for mysql GROUP_CONCAT function.
Select data with joins, group it by question_id and use group_concat on people and trips columns.
Upvotes: 1