2017-06-02 77 views
0

我有以下查詢哪些工程很好。但是,我需要獲取郵件地址的實際狀態描述(來自Code_System_State查找表),而不是它的查找代碼c.ContactMailingStateUlink。將連接添加到現有查詢中的lookkup表中?

SELECT 
    c.ClientID 
    , cp.ClientULink 
    , cp.ProgramULink 
    , c.FirstName 
    , c.LastName 
    , c.ContactMailingAddress1 
    , c.ContactMailingAddress2 
    , c.ContactMailingCity 
    , c.ContactMailingStateULink 
    , c.ContactMailingZip 
    , c.ContactEmail 
    , c.ContactHomePhone 
    , c.ContactCellularPhone 
    , StartDate 
    , EndDate 
    , cp.ProgramStatusULink 
from Client_Program cp 
INNER JOIN client c ON c.ulink = cp.clientulink 
where convert(char(10),cp.StartDate,120) = '2016-02-01' 
    and convert(char(10),cp.EndDate,120) = convert(CHAR(10),DateAdd(yyyy, 1, cp.StartDate)-1,120) 

我想修改我的查詢,如下所示,但得到一個錯誤:「無法綁定‘

’的多部分標識符」 cp.clientulink

SELECT 
    c.ClientID 
    , cp.ClientULink 
    , cp.ProgramULink 
    , c.FirstName 
    , c.LastName 
    , c.ContactMailingAddress1 
    , c.ContactMailingAddress2 
    , c.ContactMailingCity 
    , c.ContactMailingStateULink 
    , css.Description 
    , c.ContactMailingZip 
    , c.ContactEmail 
    , c.ContactHomePhone 
    , c.ContactCellularPhone 
    , StartDate 
    , EndDate 
    , cp.ProgramStatusULink 
from Client_Program cp, Code_System_State css 
INNER JOIN client c ON c.ulink = cp.clientulink 
INNER JOIN client ON c.ContactMailingStateUlink = css.code 
where convert(char(10),cp.StartDate,120) = '2016-02-01' 
    and convert(char(10),cp.EndDate,120) = convert(CHAR(10),DateAdd(yyyy, 1, cp.StartDate)-1,120) 

如何正確連接這些表?謝謝。

+0

顯示錶定義爲你想加入的2張桌子。我們還沒有足夠的信息。 – ganders

+0

連接不是什麼給錯誤。在表Client_Program中的列clientulink不存在 – SAS

+1

AFAIK,你只能引用FROM語句後的一個表,但你使用兩個:'從Client_Program cp,Code_System_State css' – ksauter

回答

1

該聯接不是什麼給錯誤。 Client_Program表中的列clientulink不存在。

現在在from子句中不推薦使用舊式語法。這是更好的:

from Client_Program cp 
inner join Code_System_State css 
    ON cp.___KEY___ = css.__KEY___ 
+0

是的,我應該使用Code_System_State作爲下一次連接,而不是再次使用Client表。非常感謝您的幫助! – DataCrypt

0

我從來沒有試圖加入一個表列標題不匹配 - 這可能是問題。您創建連接的列中的數據應匹配。 例如,如果我加盟,我可以預期將在CP標題被稱爲宇聯表和稱爲ContactMailingStateUlink 在CSS一列,這樣的代碼應該閱讀

From Client Programme 
on c.ulink=CP.ulink 
Inner Join CSS 
on c.ContactMailingStateUlink = css_code.ContactMailingStateUlink 
+0

我同意標題應該匹配,但這是數據庫由供應商設計的方式。但是,它們確實包含相同的查找數據,因此連接是正確的。再次感謝你。 – DataCrypt