如何将此Access Query转换为mySQL查询?

时间:2013-12-18 15:16:26

标签: mysql sql database ms-access ms-access-2010

鉴于数据库中初学者的复杂查询,如何将此访问sql转换为mysql? 这是MS ACCESS查询:

SELECT tblSections.[Course Code], tblSections.Section, tblSections.Day, tblSections.[Start Time], tblSections.[End Time], tblSections.Room, tblProfessors.[Last Name], tblProfessors.[First Name], Count(tblStudentsCoursesSections.[Student ID]) AS Enrolled, ([tblCourses].[Max])-[Enrolled] AS Slots
    FROM tblCourses INNER JOIN (tblStudentsCoursesSections RIGHT JOIN (tblProfessors INNER JOIN tblSections ON tblProfessors.[ID Number] = tblSections.[Professor ID]) ON (tblStudentsCoursesSections.[Course Code] = tblSections.[Course Code]) AND (tblStudentsCoursesSections.Section = tblSections.Section)) ON tblCourses.[Course Code] = tblSections.[Course Code]
    GROUP BY tblSections.[Course Code], tblSections.Section, tblSections.Day, tblSections.[Start Time], tblSections.[End Time], tblSections.Room, tblProfessors.[Last Name], tblProfessors.[First Name], tblStudentsCoursesSections.[Course Code], tblStudentsCoursesSections.Section, tblCourses.Max
    HAVING (((tblSections.[Course Code])="SOCTEC2"));

enter image description here

我尝试通过将`替换为`来转换它,但它不起作用。有什么不对?这是我在命令行中输入mySQL的内容。还会显示相应的错误。

SELECT tblSections.`Course Code`, tblSections.Section, tblSections.Day, tblSections.`Start Time`, tblSections.`End Time`, tblSections.Room, tblProfessors.`Last Name`, tblProfessors.`First Name`, Count(tblStudentsCoursesSections.`Student ID`) AS Enrolled, (`tblCourses`.`Max`)-`Enrolled` AS Slots FROM tblCourses INNER JOIN (tblStudentsCoursesSections RIGHT JOIN (tblProfessors INNER JOIN tblSections ON tblProfessors.`ID Number` = tblSections.`Professor ID`) ON (tblStudentsCoursesSections.Section = tblSections.Section) AND (tblStudentsCoursesSections.`Course Code` = tblSections.`Course Code`)) ON tblCourses.`Course Code` = tblSections.`Course Code` GROUP BY tblSections.`Course Code`, tblSections.Section, tblSections.Day, tblSections.`Start Time`, tblSections.`End Time`, tblSections.Room, tblProfessors.`Last Name`, tblProfessors.`First Name`, tblStudentsCoursesSections.`Course Code`, tblStudentsCoursesSections.Section, tblCourses.Max HAVING (((tblSections.`Course Code`)="SOCTEC2"));

注册学生的计数是否会在注册的字段中显示?

enter image description here

UPDATE !!

I did what you suggested but I reverted back to HAVING keyword since WHERE results to syntax error. 

SELECT 
    tblSections.`Course Code`,
    tblSections.`Section`,
    tblSections.`Day`,
    tblSections.`Start Time`,
    tblSections.`End Time`,
    tblSections.`Room`,
    tblProfessors.`Last Name`,
    tblProfessors.`First Name`,
    COUNT(tblStudentsCoursesSections.`Student ID`) AS `Enrolled`,
    /* Since Enrolled was just defined as an alias in this scope you cannot use it 
      in the SELECT yet, but you can do the aggregate COUNT again */
    (`tblCourses`.`Max` - COUNT(tblStudentsCoursesSections.`Student ID`)) AS `Slots`
FROM
    # Order your table joins according to the pairs used in ON clauses...
    tblCourses
    INNER JOIN tblSections ON tblCourses.`Course Code` = tblSections.`Course Code`
    RIGHT JOIN tblProfessors ON tblProfessors.`ID Number` = tblSections.`Professor ID`
    INNER JOIN tblStudentsCoursesSections 
        ON ((tblStudentsCoursesSections.Section = tblSections.Section)
        AND (tblStudentsCoursesSections.`Course Code` = tblSections.`Course Code`))
GROUP BY 
    tblSections.`Course Code`,
    tblSections.`Section`,
    tblSections.`Day`,
    tblSections.`Start Time`,
    tblSections.`End Time`,
    tblSections.`Room`,
    tblProfessors.`Last Name`,
    tblProfessors.`First Name`,
    /* These are not in your SELECT list and so should probably not be in the GROUP BY
    tblStudentsCoursesSections.`Course Code`,
    tblStudentsCoursesSections.`Section`,
     Might need to group on `Slots` instead of tblCourses.Max */
    tblCourses.Max
/* This should be a WHERE rather than HAVING since it does not operate on an aggregate */
HAVING
    /* Single quotes preferred for string literals */
    tblSections.`Course Code` = 'SOCTEC2';

不幸的是,没有注册学生的部分没有出现。为什么会这样?

enter image description here

1 个答案:

答案 0 :(得分:2)

MS Access有一个不寻常的要求,JOIN被包含在一组混乱的嵌套()中。你需要做的是通过检查各种ON条件然后适当地排列它们来解开哪些表成对连接

我还将其余的列和别名包含在GROUP BY里面的反引号中,在那里你丢失了一些。

SELECT 
    tblSections.`Course Code`,
    tblSections.`Section`,
    tblSections.`Day`,
    tblSections.`Start Time`,
    tblSections.`End Time`,
    tblSections.`Room`,
    tblProfessors.`Last Name`,
    tblProfessors.`First Name`,
    COUNT(tblStudentsCoursesSections.`Student ID`) AS `Enrolled`,
    /* Since Enrolled was just defined as an alias in this scope you cannot use it 
      in the SELECT yet, but you can do the aggregate COUNT again */
    (`tblCourses`.`Max` - COUNT(tblStudentsCoursesSections.`Student ID`)) AS `Slots`
FROM
    # Order your table joins according to the pairs used in ON clauses...
    tblCourses
    INNER JOIN tblSections ON tblCourses.`Course Code` = tblSections.`Course Code`
    /* This RIGHT JOIN is assumed to be returning all records from tblProfessors regardless
       of match in tblSections. If that is the opposite of what was intended, change to LEFT JOIN */
    RIGHT JOIN tblProfessors ON tblProfessors.`ID Number` = tblSections.`Professor ID`
    INNER JOIN tblStudentsCoursesSections 
        ON (tblStudentsCoursesSections.Section = tblSections.Section)
        AND (tblStudentsCoursesSections.`Course Code` = tblSections.`Course Code`)
/* This should be a WHERE rather than HAVING since it does not operate on an aggregate */
WHERE 
    /* Single quotes preferred for string literals */
    tblSections.`Course Code` = 'SOCTEC2';
GROUP BY 
    tblSections.`Course Code`,
    tblSections.`Section`,
    tblSections.`Day`,
    tblSections.`Start Time`,
    tblSections.`End Time`,
    tblSections.`Room`,
    tblProfessors.`Last Name`,
    tblProfessors.`First Name`,
    /* These are not in your SELECT list and so should probably not be in the GROUP BY
    tblStudentsCoursesSections.`Course Code`,
    tblStudentsCoursesSections.`Section`,
     Might need to group on `Slots` instead of tblCourses.Max */
    `Slots`

附录

  

不幸的是,没有注册学生的部分没有出现。为什么会这样?

由于INNER JOINtblStudentsCoursesSections之间有tblSectionstblStudentsCoursesSections中的任何记录都不会从结果中删除该部分。要确保无论如何都要返回该部分,请使用LEFT JOIN tblStudentsCoursesSections代替INNER JOIN tblStudentsCoursesSections。由于tblSections位于联接的“左侧”,即使没有匹配也会返回。