Left Join On和不支持的子句

时间:2016-09-11 02:50:19

标签: sql access

我查看过各种帖子(this onethat onethis other one)并认为我得到了答案。

LEFT JOIN之后,我可能会添加ON [condition] AND [other condition](我也尝试了WHERE)。但是电脑说没有。 Access一直说不支持 join表达式

考虑下面的student_records表:

STUDENTCODE  | SEMESTERINDEX
12345        | 20112
12345        | 20113
12345        | 20121
67890        | 0
67890        | 20111
67890        | 20112

我想从我的SEMESTERINDEX表中找到每个学生的最小students,大于20001. (下面的记录可能是错误的,0和1 SEMESTERINDEX是用于转移的学分。)

我正在使用访问权限,因此SQL中有VBA功能。我还加入了几个表,我引用了整个查询。

SELECT students.STUDENTCODE, prefixes.PREFIXNAMEENG, 
students.STUDENTNAMEENG, students.STUDENTSURNAMEENG, levels.level_name, programs.PROGRAMNAMEENG, calendars.calendar_load, 
MAX(student_records.SEMESTERINDEX) AS latest_semester, MIN(student_records.SEMESTERINDEX) AS intake_semester,
FROM student_records LEFT JOIN (
    (
        (
            (
                (students LEFT JOIN prefixes ON students.PREFIXID = prefixes.PREFIXID) 
                LEFT JOIN levels ON students.LEVELID = levels.level_id) 
            LEFT JOIN programs ON students.PROGRAMID = programs.PROGRAMID) 
        LEFT JOIN calendar_conversion ON students.SCHEDULEGROUPID = calendar_conversion.schedule_id) 
    LEFT JOIN calendars ON calendar_conversion.calendar_id = calendars.calendar_id) ON student_records.STUDENTCODE = students.STUDENTCODE AND student_records.SEMESTERINDEX> 2001
GROUP BY students.STUDENTCODE, prefixes.PREFIXNAMEENG, students.STUDENTNAMEENG, students.STUDENTSURNAMEENG, levels.level_name, programs.PROGRAMNAMEENG, calendars.calendar_load;

所以我错放了AND student_records.SEMESTERINDEX > 2001

1 个答案:

答案 0 :(得分:0)

哦,我从这些括号和疯狂的缩进中拯救了我。

这是你如何做到的。所有括号在SQL中都不重要

SELECT 
  students.STUDENTCODE, 
  prefixes.PREFIXNAMEENG, 
  students.STUDENTNAMEENG, 
  students.STUDENTSURNAMEENG, 
  levels.level_name, 
  programs.PROGRAMNAMEENG, 
  calendars.calendar_load, 
  minmax.latest_semester, 
  minmax.intake_semester,
FROM student_records 
LEFT JOIN (
   SELECT 
     studentcode,
     MAX(student_records.SEMESTERINDEX) AS latest_semester, 
     MIN(student_records.SEMESTERINDEX) AS intake_semester
   FROM students
   WHERE students.STUDENTCODE > 2001
   GROUP BY studentcode
) as MinMax ON student_records.STUDENTCODE = minmax.STUDENTCODE
LEFT JOIN students ON student_records.STUDENTCODE = students.STUDENTCODE 
LEFT JOIN prefixes ON students.PREFIXID = prefixes.PREFIXID
LEFT JOIN levels ON students.LEVELID = levels.level_id
LEFT JOIN programs ON students.PROGRAMID = programs.PROGRAMID
LEFT JOIN calendar_conversion ON students.SCHEDULEGROUPID = calendar_conversion.schedule_id
LEFT JOIN calendars ON calendar_conversion.calendar_id = calendars.calendar_id

这在sql中称为子查询,它允许您在子集上执行分组,然后将其连接回其余数据。

我认为你错了认为有一些关于需要过滤器的连接 - 实际上是你加入的数据需要过滤。