【问题标题】:How do I convert this Access Query to mySQL query?如何将此访问查询转换为 mySQL 查询?
【发布时间】:2014-01-06 20:20:13
【问题描述】:

给定一个数据库初学者的复杂查询,我如何将这个访问 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"));

我尝试通过将 [] 替换为 `` 来转换它,但它不起作用。有什么不对?这是我在命令行的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"));

不就是在注册字段中显示注册学生的人数吗?

更新!!

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';

很遗憾,没有出现注册学生为零的部分。为什么会这样?

【问题讨论】:

  • 第一,看你的问题,我的眼睛在流血,格式化,第二,什么错误?
  • 也许从教授开始。从概念上讲,它可能更有意义。
  • 抱歉,WHERE 语法错误是因为它必须在 GROUP BY 之前。它真的应该是 WHERE 而不是 HAVING 所以过滤器在所有连接之前应用,性能显着提高......

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


【解决方案1】:

MS Access 有一个不寻常的要求,即JOINs 被包含在一组凌乱的嵌套() 中。您需要在这里做的是通过检查各种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`

附录

很遗憾,没有出现注册学生为零的部分。为什么会这样?

由于您在tblStudentsCoursesSectionstblSections 之间有一个INNER JOIN,因此tblStudentsCoursesSections 中的任何记录都不会从结果中删除该部分。为确保无论如何返回该部分,请使用LEFT JOIN tblStudentsCoursesSections 代替INNER JOIN tblStudentsCoursesSections。由于tblSections 在连接的“左侧”,即使没有匹配也会返回。

【讨论】:

  • 如何根据 ON 子句中使用的对对表连接进行排序? mySQL中的优先级是从左到右吗? mySQL中也不允许括号给出优先级?
  • 我的意思是例如:tblCourses INNER JOIN tblSections ON tblCourses.Course Code = tblSections.Course Code RIGHT JOIN tblProfessors ON tblProfessors.ID Number = tblSections.@987654先右连接还是先内连接?
  • According to the docs 如果需要,您可以使用括号嵌套连接。根据我的经验,这并不经常需要,并且 5.0.1 之前的 MySQL 删除 () 和左关联所有连接的行为通常就足够了。 所以如果您没有像我一样从查询中得到您想要的结果,您可能需要尝试使用 RIGHT JOIN 的优先级 - 这似乎没有必要。
  • 我添加了一点来解决您的后续问题。
猜你喜欢
  • 2019-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
相关资源
最近更新 更多