当我在SQL语句中只有一个内部联接时,它完美地运行了。我尝试加入第二个表,现在我收到一个错误,指出存在语法错误(缺少运算符)。这有什么不对?
adsFormView.SelectCommand = "SELECT * FROM [tableCourse] INNER JOIN [tableGrade] ON [tableCourse].[grading] = [tableGrade].[id] INNER JOIN [tableCourseType] ON [tableCourse].[course_type] = [tableCourseType].[id] WHERE [prefix]='" & myPrefix & "' AND [course_number]='" & myCourseNum & "'"
答案 0 :(得分:146)
对于多表连接,您必须将多余的连接嵌套在括号中:
SELECT ...
FROM ((origintable
JOIN jointable1 ON ...)
JOIN jointable2 ON ...)
JOIN jointable3 ON ...
基本上,对于你加入第一个的额外表,你需要在原始'FROM'表之前有一个括号,在匹配的JOIN'on'子句上需要一个结束括号。
答案 1 :(得分:22)
MS Access(特别是Jet / ACE)需要具有多个连接的括号。尝试:
adsFormView.SelectCommand = _
" SELECT * FROM ([tableCourse] " & _
" INNER JOIN [tableGrade] " & _
" ON [tableCourse].[grading] = [tableGrade].[id]) " & _
" INNER JOIN [tableCourseType] " & _
" ON [tableCourse].[course_type] = [tableCourseType].[id] " & _
" WHERE [prefix]='" & myPrefix & "'" & _
" AND [course_number]='" & myCourseNum & "'"
答案 2 :(得分:0)
尽管使用MS SQL Server,MS Access仍需要为多个JOIN语句加上括号。基本上,JOIN是两个表之间的操作。实际上,当您有多个JOIN时,您就是将前一个JOIN的结果联接到另一个表中。对于任何额外的JOIN,此逻辑都是级联的。例如,如果您在4个表之间进行JOIN操作,则需要如下编写:
SELECT * FROM
(
( Table1 JOIN Table2 ON Table1.column1 = Table2.column2) --result of JOIN is treated as a temp table
JOIN Table3 ON Table1.column1 = Table3.column3
) --result of JOIN is treated as another temp table
JOIN Table4 ON Table4.column4 = Table2.column2