语法错误(缺少运算符)

时间:2012-01-20 21:30:55

标签: sql ms-access

我不是一个训练有素的程序员,所以努力做到这一点很困难。我用这个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

2 个答案:

答案 0 :(得分: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 :(得分:1)

当SELECT语句包含多个连接时,

Access'db引擎对括号有严格要求。我建议你从查询的简化版本开始(丢弃字段列表和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引擎不接受的语法创建查询变得困难。

编辑:如果此查询的目的是比较salesforce和agents表中的agentname值,您可能希望尝试将其作为WHERE子句:

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