Answer the question
In order to leave comments, you need to log in
Is it possible to "expand" a sql table?
I have a table like this:
| ID | ProductID | PropName | PropVal |
----------------------------------------------
| 1 | 560 | Color | green |
| 2 | 560 | Family | Resistors |
| 3 | 560 | Series | 375 |
| ID | ProductID | Color | Family | Series |
---------------------------------------------------
| 1 | 560 | green | Resistors | 375 |
Answer the question
In order to leave comments, you need to log in
There is no simple solution, but the script should not be heavy, i.e. take all unique ProductIDs from the table, select rows from the table for each, create an array where key is PropName and value is PropVal, then write everything to a new table, where you create from the array columns named key and value value, I would write everything to MongoDB
- on the one hand, your desired structure is closer to a document-oriented database than a relational one (maybe with which mongo to experiment),
- on the other hand, if it is psql, then you can use json field (version 9.2, this functionality was expanded in 9.3)
- on the third hand, no one It prevents you from immediately writing to the desired structure by creating a table with 50 fields
. But I don’t know a simple and quick way to take and expand like this. I myself will listen with pleasure to the answer if there is one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question