我刚刚将我的数据库从MS SQL迁移到了MySQL。
运行报告时,我现在收到以下错误:
Application Execution Exception
Error Type: database : 0
Error Messages: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2 YEAR(res_created) AS responseYear FROM Responses INNER JOIN ' at line 1
SQL已发送:
SELECT DISTINCT TOP #arguments.yearsToReturn# YEAR(res_created) AS responseYear
FROM
Responses
INNER JOIN
Questions ON Responses.question_id = Questions.question_id
INNER JOIN
Lookup_Survey_Questions ON Questions.question_id = Lookup_Survey_Questions.question_id
INNER JOIN
Survey ON Lookup_Survey_Questions.survey_id = Survey.survey_id
INNER JOIN
School ON Survey.sch_id = School.sch_id
INNER JOIN
Authority ON School.auth_id = Authority.auth_id
WHERE
Authority.auth_id = #arguments.authorityID#
AND
Questions.question_id = #arguments.questionID#
AND
Responses.survey_id IN (SELECT Survey.survey_id FROM Survey where Survey.sch_id in (SELECT School.sch_id FROM School where auth_id=#arguments.authorityID#))
ORDER BY
responseYear ASC
我该如何解决?
由于
答案 0 :(得分:1)
“Top 2”不是MySQL语法。要选择前两个结果,请将限制2添加到查询结尾:
...
ORDER BY
responseYear ASC LIMIT 2
答案 1 :(得分:1)
你在一个地方使用这两个条款。您使用的是DISTINCT和TOP,这就是错误的原因。
使用此查询:
SELECT DISTINCT YEAR(res_created) AS responseYear
FROM Responses
INNER JOIN Questions ON Responses.question_id = Questions.question_id
INNER JOIN Lookup_Survey_Questions ON Questions.question_id = Lookup_Survey_Questions.question_id
INNER JOIN Survey ON Lookup_Survey_Questions.survey_id = Survey.survey_id
INNER JOIN School ON Survey.sch_id = School.sch_id INNER JOIN Authority ON School.auth_id = Authority.auth_id
WHERE Authority.auth_id = 5 AND Questions.question_id = 20 AND Responses.survey_id IN (
SELECT Survey.survey_id FROM Survey where Survey.sch_id in (SELECT School.sch_id FROM School where auth_id=5))
ORDER BY responseYear ASC
LIMIT 0,2; //use the limit
答案 2 :(得分:1)
我认为这可以简化如下......
SELECT DISTINCT YEAR(res_created) responseYear
FROM Responses r
JOIN Questions q
ON q.question_id = r.question_id
JOIN Lookup_Survey_Questions lsq
ON lsq.question_id = q.question_id
JOIN Survey u
ON u.survey_id = lsq.survey_id
JOIN School c
ON c.sch_id = u.sch_id
JOIN Authority a
ON a.auth_id = c.auth_id
WHERE a.auth_id = #arguments.authorityID#
AND q.question_id = #arguments.questionID#
AND c.auth_id=#arguments.authorityID#
ORDER
BY responseYear ASC
LIMIT 2;