2012-01-20 114 views
4

我不是一名訓練有素的程序員,所以試圖做到這一點很困難。我得到了這個SQL代碼的上述錯誤,我找不出原因。請指教。語法錯誤(缺少操作符)

SELECT a.agentname as 'Salesforce AgentName', a.loannumber, ag.agentname as 'portal agentname' 
from salesforce a 
inner join portal b ON a.loannumber = b.loannumber 

left join agents ag ON b.agentid = ag.agentid 
where a.agentname <> ag.agentname 
+0

嘗試改變<>來! = –

+0

[看起來您可能需要一些括號](http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html) –

+0

我仍然收到該更改的錯誤。 – user1161512

回答

2

位猜測的(based on here

SELECT a.agentname AS 'Salesforce AgentName', 
     a.loannumber, 
     ag.agentname AS 'portal agentname' 
FROM ((salesforce as a) 
     INNER JOIN portal as b 
     ON a.loannumber = b.loannumber) 
     LEFT JOIN agents ag 
     ON b.agentid = ag.agentid 
WHERE a.agentname <> ag.agentname 

BTW它通常是指外連接表列WHERE子句中的錯誤,除非你考慮的可能性,他們是NULL

1

當SELECT語句包含多於1個連接時,Access數據庫引擎對括號有嚴格的要求。我建議你從查詢的簡化版本開始(放棄字段列表和WHERE子句),這樣你就可以專注於獲取正確的連接。

SELECT * 
FROM 
    (salesforce AS a 
    INNER JOIN portal AS b 
    ON a.loannumber = b.loannumber) 
    LEFT JOIN agents AS 
    ag ON b.agentid = ag.agentid; 

假設版本的作品,試試這個:

SELECT 
    a.agentname AS [Salesforce AgentName], 
    a.loannumber, 
    ag.agentname AS [portal agentname] 
FROM 
    (salesforce AS a 
    INNER JOIN portal AS b 
    ON a.loannumber = b.loannumber) 
    LEFT JOIN agents AS 
    ag ON b.agentid = ag.agentid 
WHERE a.agentname <> ag.agentname; 

通知我包圍的別名用方括號,而不是單引號的列名。單引號不會觸發錯誤,但它們將包含在列標題中......我懷疑你想要的。

您可以在Access的查詢設計器中構建Access查詢來幫助自己。這將使得使用db引擎不會接受的語法創建查詢變得困難。

編輯:如果此查詢的目的是比較從銷售人員和代理表AGENTNAME值,可能你會想嘗試以此爲WHERE子句:

WHERE a.agentname <> ag.agentname OR ag.agentname Is Null;