【发布时间】: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