访问SQL:内部join多个表
我有一个数据库中的多个表:
tblOjt
ID studentid courseid companyid addresseeid dateadded datestarted dateended ojthours 1 3 1 1 1 9/25/2013 500
tblStudent
ID lastname firstname middlename course gender renderedhours dateadded archive 3 Dela Cruz Juan Santos BSIT Male 500
tblCourse
ID coursealias coursename hours 1 BSIT Bachelor of Science in Information Technology 500
tblCompany
ID companyname 1 MyCompany
tblAddressee
ID addresseename 1 John dela Cruz
我需要一个SQL语句,我可以得到这个值:
tableOjt.id tableOJT.surname,firstname, and middlename course companyname addresseename dateadded datestarted dateended ojthours
我怎么会得到这个代码在SQL使用这些连接方法…即时通讯编写它在VB6 ADODC,这是在一个标准的SQL相同的语法? 谢谢
如果您正在编写针对Access数据库后端的查询,则需要使用以下连接语法:
select t1.c1 , t2.c2 , t3.c3 , t4.c4 from ((t1 inner join t2 on t1.something = t2.something) inner join t3 on t2.something = t3.something) inner join t4 on t3.something = t4.something
表和列的名字在这里并不重要,但括号的位置是。 基本上,除了第一个,除了第一个,其中n是连接在一起的表的数量之外,您需要在每个新join
子句开始之前,在子句之后有n-2个左括号。
原因是Access的连接语法一次只能连接两个表,所以如果你需要连接两个以上的表,你需要用括号括起来。
SELECT tblOjt.id, tblStudent.firstname, tblStudent.middlename, tblStudent.lastname, tblStudent.course, tblCompany.companyname, tblAddressee.addressee FROM (((tblOjt INNER JOIN tblStudent ON tblOjt.studentid = tblStudent.id) INNER JOIN tblCourse ON tblOjt.courseid = tblCourse.id) INNER JOIN tblCompany ON tblOjt.companyid = tblCompany.id) INNER JOIN tblAddressee ON tblOjt.addresseeid = tbladdressee.id
发现它!感谢Yawar的方法…
一直试图使用VBA运行这个SQL,但不会运行使用DoCmd.RunQuery。 我已经尝试了SQL,但它的工作。
str = "SELECT tbl_company.[Company], tbl_company.[Commodity], tbl_company.[Segment], tbl_company.[MainProduct]," & _ " tbl_financials.[DataYear]," & _ " mstr_financial.[FinancialData]," & _ " tbl_financials.[Amount]," & _ " tbl_financials.[Unit]," & _ " tbl_company.[CompanyID]" & _ " FROM (tbl_company" & _ " INNER JOIN tbl_financials ON tbl_company.[CompanyID] = tbl_financials.[CompanyID])" & _ " INNER JOIN mstr_financial ON tbl_financials.[FinID] = mstr_financial.[FinID] " & _ " ORDER BY tbl_company.[Company], tbl_financials.[DataYear] DESC"