2

IF条件に従ってwhere句を変更しようとしています。何らかの理由でクエリが失敗します。助けていただければ幸いです。

ありがとう!

        SELECT
            c.id AS course,
            cr.id AS criteriaid,
            u.id AS userid,
            ue.timestart AS otimestart,
            (ue.timestart + cr.enrolperiod) AS ctimestart,
            ue.timecreated AS otimeenrolled,
            (ue.timecreated + cr.enrolperiod) AS ctimeenrolled
        FROM
            {user} u
        INNER JOIN
            {user_enrolments} ue
         ON ue.userid = u.id
        INNER JOIN
            {enrol} e
         ON e.id = ue.enrolid
        INNER JOIN
            {course} c
         ON c.id = e.courseid
        INNER JOIN
            {course_completion_criteria} cr
         ON c.id = cr.course
        LEFT JOIN
            {course_completion_crit_compl} cc
         ON cc.criteriaid = cr.id
        AND cc.userid = u.id
        WHERE
            cr.criteriatype = 5
        AND c.enablecompletion = 1
        AND cc.id IS NULL
        AND             
        (
        IF (ue.timeextension IS NULL)
                (ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
          OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?)
        ELSE 
                (ue.timestart > 0 AND ue.timestart + ue.timextension < ?
          OR ue.timecreated > 0 AND ue.timecreated + ue.timeextension < ?)
        ) 
4

3 に答える 3

2

式の構文は次のとおりIFです。

IF(<cond-expr>, <then-expr>, <else-expr>)

構文にキーワードはありませんELSE。結果の 2 つの式をコンマで区切るだけです。したがって、式の正しい構文は次のようになります。

AND IF(ue.timeextension IS NULL,
       (ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?)
         OR (ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?),
       (ue.timestart > 0 AND ue.timestart + ue.timextension < ?)
         OR (ue.timecreated > 0 AND ue.timecreated + ue.timeextension < ?))

これを書く別の方法は次のとおりです。

AND ((ue.timestart > 0 AND ue.timestart + IFNULL(ue.timeextesion, cr.enrolperiod) < ?)
     OR (ue.timecreated > 0 AND ue.timecreated + IFNULL(ue.timeextesion, cr.enrolperiod)) < ?)
于 2012-10-29T19:37:32.247 に答える
0

達成したい内容に応じて、正しい where 句は次のようになります。

...
where ...
and (if(ue.timeextension IS NULL,
        ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
            OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?,
        ue.timestart > 0 AND ue.timestart + ue.timextension < ?
            OR ue.timecreated > 0 AND ue.timecreated + ue.timeextension < ?);
于 2012-10-29T19:35:57.310 に答える
0

where ieの最後のセクションを試すことができると思います

   AND             
    (
    IF (ue.timeextension IS NULL)
            (ue.timestart > 0 AND ue.timestart + cr.enrolperiod < ?
      OR ue.timecreated > 0 AND ue.timecreated + cr.enrolperiod < ?)
    ELSE 
            (ue.timestart > 0 AND ue.timestart + ue.timextension < ?
      OR ue.timecreated > 0 AND ue.timecreated + ue.timeextension < ?)
    ) 

なので

   AND             
    (
      (ue.timestart > 0 AND ue.timestart + 
             IFNULL(ue.timeextension , cr.enrolperiod )  < ? )
     OR (ue.timecreated > 0 AND ue.timecreated + 
             IFNULL (ue.timeextension, cr.enrolperiod) < ? )
    ) 
于 2012-10-29T19:38:55.860 に答える