I just need some direction on this. I have the following tables:
Table: entity
- ID (INT)
Table: attributes
- ID (INT)
- name (VARCHAR)
- internalname (VARCHAR)
Table: values
- ID (INT)
- entity (ID)
- attributes (INT)
- value (Text)
What I want is to make a Select statment that will return something like the following:
- ID = entity.ID
- {attributes.internalname ID 1} = values.{attribe internalname 1}.value
- {attributes.internalname ID 2} = values.{attribe internalname 2}.value
- {attributes.internalname ID 3} = values.{attribe internalname 3}.value
- {attributes.internalname ID n} = values.{attribe internalname n}.value
- etc...
It would be like combining:
SELECT entity.id FROM entity;
and
SELECT (SELECT values.value FROM values WHERE values.entity = entity.ID AND values.attributes = attributes.ID) FROM attributes;
It is a difficult thing to explain, however if you need me to explain further, please let me know.
I effectively want to rotate all values in attributes to columns and turn all the values into its corresponding attribute’s value with the ID as the selector.
I give the query an ID (The element ID), and in one result row it returns all the data.
Thanks in Advance!
You can’t create columns dynamically, so you need to know beforehand what you want as columns.
If attributes (1,2,3,4) represent (firstname, lastname, extraname, additionalname) you can query it like this:
or
You can also use the GROUP_CONCAT to return the values in a comma separated list in one column.
Oh, and
valuesis a reserved word. Don’t use it as table name.Oh 2, don’t use this model unless you really really have to. You will pay a big juicy price in terms of performance and data consistency.