使用内连接,左外连接,交叉应用获取语法错误与 Where 子句

Posted

技术标签:

【中文标题】使用内连接,左外连接,交叉应用获取语法错误与 Where 子句【英文标题】:Using inner join, left outer join, cross apply get syntax error with Where Clause 【发布时间】:2018-06-03 12:52:17 【问题描述】:

我们有一个第三方数据库。联系信息包含在许多表中:contact1、contact2、contsupp。使用内连接、左外连接、交叉应用与 Where 子句获取语法错误。该查询在没有左外连接的情况下工作。但我需要它来选择正确的电子邮件。在这种情况下,where 子句应该放在哪里?

            SELECT 'P', 
            'NA' PEOPLE_ID,
            'NA' PEOPLE_CODE_ID, 
            T.ForeName, 
            T.Middle_Name, 
            T.Surname, 
            'PERM', 
            'N', 
            GetDate(), 
            GetDate(), 
            'SCTBRDG', 
            '0001', 
            GetDate(), 
            GetDate(), 
            'SCTBRDG', 
            '0001', 
            '*', 
            C2.UBIRTHDATE, 
            C2.Ubrthcnty, 
            CASE WHEN LEFT([URELEASE], 6) IN ('Employer', 'EMPLYR') THEN 'EMPLYR' ---was URELEASE
             WHEN LEFT([URELEASE], 6) IN ('Family', 'Sister', 'OFAM') THEN 'OFAM'
             WHEN LEFT([URELEASE], 6) = 'FATHER' THEN 'FATHER'
             WHEN LEFT([URELEASE], 6) IN ('Friend', 'OTHER') THEN 'OTHER'
             WHEN LEFT([URELEASE], 6) IN ('Guardian', 'GUARDN') THEN 'GUARDN'
             WHEN LEFT([URELEASE], 6) = 'MOTHER' THEN 'MOTHER'
             WHEN LEFT([URELEASE], 6) IN ('PARENT', 'Parents') THEN 'PARENT'
             WHEN LEFT([URELEASE], 6) = 'RESTR' THEN 'RESTR'
             WHEN LEFT([URELEASE], 6) = 'Spouse' THEN 'Spouse'
             WHEN LEFT([URELEASE], 6) IS NULL THEN 'NROF'
             ELSE 'NONE' END AS ReleaseInfo, 
            C2.Ugender, 
            C2.Umarstat, 
            C2.Udenominat, 
            C2.Uveteran, 
            C2.Ucntrycitz, 
            C2.Uvisatype, 
            i.ACCOUNTNO, 
            i.ADDRESS1, 
            i.ADDRESS2, 
            i.ADDRESS3, 
            i.CITY, 
            i.STATE, 
            i.ZIP, 
            i.PHONE3, 
            i.FAX, 
            C2.UCOUNTY, 
            ISNULL(C2.UEMAILSCT, replace(lower(T.ForeName) + '.' + lower(T.Surname) + '@okwstudents.edu', ' ', '')),
            cs.CONTSUPREF as EMAIL,                 --Personal Email
            C2.UADDRTYPE, 
            i.KEY2, 
            i.KEY5, 
            i.KEY3, 
            C2.UCURRICULM, 
            C2.UMAJOR, 
            i.DEPARTMENT, 
            C2.UCOLLCMPLT, 
            C2.UFULLPART, 
            C2.UCOHORT, 
            C2.UAPPLICDT, 
            C2.USTUSTATDT, 
            C2.UDECISCODE, 
            C2.UDECISDATE, 
            C2.USTRTTRMDT, 
            C2.UCOHRTSTDT, 
            C2.UADMCOUNCD, 
            C2.UADVISMNTR, 
            i.Phone1, 
            i.Phone2, 
            Uethnicbg, 
            i.COUNTRY,
            C2.USOCSECNUM GOVERNMENT_ID,
            i.KEY1,
            C2.Urelease RELEASE_INFO,
            C2.USOCSECNUM TAX_ID, ---
            C2.USOCSECNUM, ---
            GETDATE()
            FROM 
            --inserted i ---test inserted, 
            GoldMine_test.dbo.CONTACT1 i
            Inner Join CONTACT2 C2 on (i.ACCOUNTNO = C2.ACCOUNTNO)
            CROSS APPLY dbo.NameParser(i.Contact) T
            --inner join GoldMine_test.dbo.CONTACT2 c2 on ( i.ACCOUNTNO = c2.ACCOUNTNO ) 
            LEFT outer join GoldMine_test.dbo.CONTSUPP cs on (  (cs.ACCOUNTNO = i.ACCOUNTNO) 
            AND (cs.RECTYPE='P') 
            AND ( cs.U_CONTACT = 'E-MAIL ADDRESS')
            WHERE i.Key1 = '31-ATE' --Incorrect syntax near the keyword 'WHERE'
            OR (i.Key1 = '50-DEP')
            OR (i.Key1 = '51-RET')

【问题讨论】:

GoldMine_test.dbo.CONTSUPP 的连接谓词中有一个额外的括号。我只想删除所有这些括号,因为它们只提供噪音和额外的击键。 除非你需要括号,否则你真的不应该使用括号。真的很容易出错。 【参考方案1】:

可能语法错误在这行代码中:

LEFT outer join GoldMine_test.dbo.CONTSUPP cs on (  (cs.ACCOUNTNO = i.ACCOUNTNO) 

尝试用这个替换它:

LEFT outer join GoldMine_test.dbo.CONTSUPP cs on (cs.ACCOUNTNO = i.ACCOUNTNO) 

并检查它是否有效

【讨论】:

以上是关于使用内连接,左外连接,交叉应用获取语法错误与 Where 子句的主要内容,如果未能解决你的问题,请参考以下文章

数据库的内连接外连接(左外连接右外连接全外连接)以及交叉连接(转)

左外连接和右外连接的区别

SQL:内连接左外连接右外连接全连接交叉连接区别

lyt经典版MySQL基础——进阶6:连接查询-sql99语法-内连接外连接交叉连接

内连接左外连接右外连接交叉连接区别

解释 MySQL 外连接内连接与自连接的区别?