我有一个 SQL Server 数据库,我需要存储未知数量的列,所以我有 2 个这样的表:
- 表
Master
,包含这些列:(id, data1, data2
固定列) unknown_fields
包含这些列的表:id_master_record, id_field, value_field
我做这样的查询:
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
我想知道是否有更好的方法来做这个查询。