I have 3 tables:
- Products, containing - id, name and description
- Attributes, containing - id, name
prod_attri_rel, containing - id, prod_id, attri_id and value
- Products looks like:
1 | test | just a description for test 2 | test2| just another description
- Attributes looks like:
1 | height 3 | length 2 | width 4 | power 5 | id
- prod_attri_rel looks like:
1 | 1 | 1 | 2 2 | 1 | 2 | 25 3 | 1 | 3 | 20 4 | 2 | 1 | 2 5 | 2 | 2 | 25 6 | 2 | 3 | 20 7 | 2 | 4 | UBEC 9 | 2 | 5 | BC2212-850
After looking at https://dba.stackexchange.com/a/33307/48727 and https://stackoverflow.com/a/695860/1820180
Now I would like to get this result:
id | name | height | width | length | maybe other attr | so on and so forth
1| test | 2 | 25 | 20 | ...
When I use the following query:
SELECT p.id, p.name , par.value, a.name FROM products p
JOIN prod_attri_rel par on p.id = par.prod_id
JOIN attributes a on par.attri_id = a.id
I get:
id | name | value| name
1 | test | 2 | height
1 | test | 25 | width
1 | test | 20 | length
Then when I tried:
SELECT p.id, p.name , group_concat( par.value ), group_concat( a.name ) FROM products p
JOIN prod_attri_rel par on p.id = par.prod_id
JOIN attributes a on par.attri_id = a.id GROUP BY p.name
I get a better result but not really it:
id | name | value | name
1 | test | 25,2,20 | width,height,length
Last I found this https://stackoverflow.com/a/10926106/1820180 but I did not understand it enough to adapt it to my problem.
Any help in this matter would be appreciated, even if it is finding another method, or getting it to work.
Purpose: This is a database for different parts to RC toys, such as motors, ESC's wheels, propellers and such, all have different attributes, but all motors have same attributes, all wheels have same attributes.