1

我刚刚将我的数据库从 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

我该如何解决?

谢谢

4

3 回答 3

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;
于 2013-07-26T10:50:53.253 回答
1

“Top 2”不是 MySQL 语法。要选择前两个结果,请在查询末尾添加限制 2:

...
    ORDER BY
        responseYear ASC LIMIT 2
于 2013-07-26T10:34:38.403 回答
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
于 2013-07-26T10:35:45.057 回答