I have a SQL Server database, I need to store an unknown number of columns, so I have 2 tables like this:
- Table
Master
, with these columns:id, data1, data2
(fixed columns) - Table
unknown_fields
with these columns:id_master_record, id_field, value_field
I do the query like this:
SELECT id, data1, data2,
(SELECT value_field FROM unknown_fields
WHERE id_master_record = Master.id AND id_field = 1) AS data3
(SELECT value_field FROM unknown_fields
WHERE id_master_record = Master.id AND id_field = 2) AS data4
(SELECT value_field FROM unknown_fields
WHERE id_master_record = Master.id AND id_field = 3) AS data5
... SO for each
FROM Master
I want to know whether there is a better way to do this query.