So I’ve got a query selecting data from 4 tables:
- cases
- taxonomies (containing titles and data about taxonomies)
- taxonomy_values (possible values for the taxonomies)
- post_taxonomy_values (holds IDs from the case table, and IDs from the taxonomy table to link the two)
I’m attempting to select a row from the cases table and then get all taxonomy names and their values. Here’s what I’ve got so far:
select
cases.title, cases.content, cases.status, taxonomies.title as 'taxonomy', taxonomy_values.value as 'taxonomy_value'
from cases, post_taxonomy_values, taxonomies, taxonomy_values
where
cases.slug = 'b-v-dpp' and
post_taxonomy_values.post = cases.id and
taxonomies.id = post_taxonomy_values.taxonomy and
taxonomy_values.id = post_taxonomy_values.value
But this is what that generates:

So basically what I’d like is to take the taxonomy names and make them column names, and set their values as part of the returned row.
Here’s the structure of the tables:
cases:

taxonomies:

taxonomy_values (‘taxonomy’ references an ID in the taxonomies table):

post_taxonomy_values (‘taxonomy’ references an ID in the taxonomies table and ‘value’ references an ID in the taxonomy_values table):

I’ve written and re-written this query but I just can’t get my head round the problem, any help would be much appreciated!
this should do the trick