從每一個獨特的返回列加入到各州:
select m.Name, s1.Enumeration as State1, s2.Enumeration as State2, s3.Enumeration as State3
from MasterTable m
left join StateTable s1 on m.State1 = s1.ID
left join StateTable s2 on m.State2 = s2.ID
left join StateTable s3 on m.State3 = s3.ID
返回從3所有狀態的1列聯接:
select m.Name, ISNULL(s1.Enumeration + ',','')
+ ISNULL(s2.Enumeration + ',','')
+ ISNULL(s3.Enumeration,'') as Enumeration
from MasterTable m
left join StateTable s1 on m.State1 = s1.ID
left join StateTable s2 on m.State2 = s2.ID
left join StateTable s3 on m.State3 = s3.ID
還有列的查詢。 ..
select m.Name,
ISNULL((select Enumeration from StateTable where ID = m.State1),'') as State1,
ISNULL((select Enumeration from StateTable where ID = m.State2),'') as State2,
ISNULL((select Enumeration from StateTable where ID = m.State3),'') as State3
from MasterTable m
+1:添加LEFT JOIN替代 – 2010-08-10 18:32:02
@ lumberjack4:您發佈的ANSI-89語法在性能上沒有區別,和福斯科的ANSI-92。 ANSI-92的好處是標準化和廣泛支持的OUTER JOIN(至少左,右)語法。 – 2010-08-10 18:33:13
謝謝你們倆..當我編輯添加第二個查詢時意識到了這一點。 – Fosco 2010-08-10 18:34:12