I am working on creating a view in MySQL for an application that tracks form submissions. There is one particular table that I cannot figure out how to write a query for. It has foreign_keys relationships with a submissions table and a table which keeps track of fields in all the pdfs used by the application. A basic overview of the table structure and application is as follows:
- The frm_form_attribute_data (the table I am struggling with) table stores information relating to a form submission and the data from a specific field on that form.
- Each entry in the frm_form_attribute_data_table has a unique id, the id of a submission, the id of a form field from a pdf, and the data that was filled in.
- An example of the attribute id: if a pdf has a "name" field, then it might have an attribute_id of 1 associated with it that will always be the "name" field on that specific pdf.
- So for every 1 form submission, there will be multiple entries in the data table, since for a given form there might be ~20-30 different, distinct fields
- The fields are differentiated with an attribute_id
The issue I am having is that I need to create a view which aggregates data based on each submission and am struggling to write queries that don't use subqueries to achieve this. The version of MySQL (5.1) that I am working with does not allow subqueries when creating views. Each row in the view should correspond to 1 unique submission_id and contain all the attribute_ids and their data as columns. So an entry for the first submission will have the submission_id, and attributes 1 through x (where x is there number of fields in that specific pdf/form) as column data. Instead of showing the attribute_id, the columns will just use the common name for that attribute(email, name, etc). I can successfully build a query which does this with the following:
SELECT submission_id, attribute_id, is_reporting, email
FROM (
SELECT form_submission_id, attribute_id, response as is_reporting
FROM frm_form_attribute_data
WHERE attribute_id = 382
) isReporting
INNER JOIN (
SELECT form_submission_id, attribute_id, response as email
FROM frm_form_attribute_data
WHERE attribute_id = 385
) email ON isReporting.form_submission_id = email.form_submission_id
Each INNER JOIN grabs the data for a specific attribute using a subquery and appends it as a column in the “view”. However, the subqueries in the FROM and INNER JOIN are not allowed in versions of MySQL < 5.7. I saw as an alternative that I can create a view for each sub query, but some of the forms have 30+ fields and I would have to create a separate view for each one. Is there a way to structure the query without using subqueries? Or will I need to create all the smaller views corresponding to a specific attribute_id.
Please bear in mind that I am unable to change the version of MySQL we are running :( There are migrations in progress but that is outside the scope for my role and team.
EXAMPLES of tables:
Table: frm_form_attribute_data
id | submission_id | attribute_id | response |
---|---|---|---|
1 | 20 | 1 | yes |
2 | 20 | 2 | [email protected] |
3 | 21 | 1 | no |
4 | 21 | 2 | NULL |
Table: Desired View
id | submission_id | isReporting | |
---|---|---|---|
1 | 20 | yes | [email protected] |
2 | 21 | no | NULL |