2011-10-02 68 views
0

我有2個表1是Lookup,另一個是Details。如何使用2個表中的數據並將其合併爲一個?

查找表

Identity Type Value 

200 Entity A 
201 Entity B 
202 Entity C 
203 Entity D 
300 SOURCE X 
301 SOURCE y 

詳細信息表

Sender(int) Reciever(int) Source(int) State(varchar) 
200    203    300    hongkong 

詳細信息表中的發件人,Reciever是實體查找表身份爲自己的ID。

我的問題是,當我寫查詢爲Select Sender,Reciever,Source,State from Details我得到200,203,300,hongkong,但我想結果爲A,D,X,hongkong。 請幫忙。

+1

您是否正在使用[「one true lookup table」](http://www.simple-talk.com/community/blogs/philfactor/archive/2008/05/29/56525 .aspx)反模式? –

+0

我不明白你在說什麼。請解釋清楚 – Ankur

+1

如果你想進一步解釋,我的評論中有一個鏈接。 –

回答

0

對這兩個表使用JOIN。它是基本的SQL語法

+0

你可以舉一些例子嗎?這將是非常有益的。 – Ankur

1
SELECT tSen.[Value] as [Sender], tRec.[Value] as [Reciever] 
, tSou.[Value] as [Source], D.[State] 
FROM Details as D 
JOIN Lookup as tSen ON D.Sender = tSen.Identity 
JOIN Lookup as tRec ON D.Reciever = tRec.Identity 
JOIN Lookup as tSou ON D.Source = tSou.Identity