我有一個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
我想知道是否有更好的方法來執行此查詢。
你的意思是'AND id_field =',而不是'AND id_master_record ='在行尾? – Blorgbeard
是的,對不起,我錯了 –