This is my table with example data:
| attr_key | attr_value | element_id |<br>
| color | black | 1 |<br>
| color | red | 2 |<br>
| color | green | 3 |<br>
| power | 82 | 1 |<br>
| power | 150 | 2 |<br>
| power | 240 | 3 |<br>
| and so on ...
How can I query this table to get a result like this:
| color | power | element_id |<br>
| black | 82 | 1 |<br>
| red | 150 | 2 |<br>
| green | 240 | 3 |
GROUP BY
does not work because it only takes the first attribute for each element_id and the others get lost.
Do I have to create a view?
Would it be a better solution to put the attributes directly into my elements
-table, e.g. in a JSON-field? These attributes are very variable, so there can be a lot of attributes for each element (way more than 20) I am not sure which solution will perform better when I have to query and check for specific attributes (all or only a few)..
Later I want to JOIN another table (elements
) using the element_id
.