I have a table called form_submission_entries
which contains data like this:
id | submission_id | field_id | value |
---|---|---|---|
1 | 1 | 1 | John |
2 | 1 | 2 | Doe |
3 | 1 | 3 | 27 |
4 | 2 | 1 | Jane |
5 | 2 | 2 | Doe |
6 | 2 | 3 | 23 |
The field_id corresponds to a form_fields
table which looks like this:
id | name | title |
---|---|---|
1 | first_name | First Name |
2 | last_name | Last Name |
3 | age | Age |
And of course a form_submissions
table which looks like this:
id | form_id | submitted_at |
---|---|---|
1 | 1 | 2021-11-05 08:16:27 |
2 | 1 | 2021-11-05 08:28:11 |
I would like to run a single query that will allow me to create a CSV file that looks like this:
submission_id | first_name | last_name | age | submitted_at |
---|---|---|---|---|
1 | John | Doe | 27 | 2021-11-05 08:16:27 |
2 | Jane | Doe | 23 | 2021-11-05 08:28:11 |
I am able to achieve this by creating multiple queries. First by getting a list of ids from the form_submissions
. Then using that list of ids to create multiple queries to fetch data from the form_submission_entries
using the submission_id
. But it looks nasty. I was hoping someone somewhere would have a much better approach. Thanks so much!