0

我有这个查询,它在SQL服务器中运行良好,但在 Access 中运行良好,我在转换它时遇到了麻烦。我一直听说JET缺少一些TSQL功能,我想复杂的连接就是其中之一。

SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID

LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
                                 AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637')
where ( c.[Position] = 'Customer Service Representative'
      OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle

我尝试将额外的连接子句放在 WHERE 子句中,但由于某种原因,这不会产生正确的记录计数。

4

2 回答 2

2

当您有多个时JOINms-access您需要用括号将它们包装起来,如下所示:

SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from (((HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID)
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID)

LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
                                 AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637'))
where ( c.[Position] = 'Customer Service Representative'
      OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle

否则您将遇到Missing Operator错误

于 2012-10-15T18:12:08.157 回答
1

检查您的表别名是否用“as”声明。Access 不喜欢 [tablename] [alias],而是尝试将 [tablename] 作为 [alias]。我知道复杂的左连接应该不是问题,但是如果 Access 返回一些连接错误,它可能会阻塞别名声明。我还会尝试查询对 ET 表的限制,然后将其加入更大的查询。我注意到尝试对左连接或右连接所涉及的记录施加限制通常不会产生正确的记录,因为 Access 会在连接后限制集合。

于 2012-10-15T18:09:14.440 回答