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

ある場所で two 句を使用しています。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 に答える
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 に答える