mySQL IF语句语法

时间:2014-02-14 13:35:18

标签: mysql if-statement

这是MySQL,它抱怨if语句。也许有人看到了这个错误,因为我觉得我看了太长时间了:

SELECT c.id AS id, (p.validFor * 30) AS validFor, p.validFrom as validFrom, 
date(FROM_UNIXTIME(rr.timemodified)) as date, 
( CASE 
    WHEN date(validFrom) < date(FROM_UNIXTIME(rr.timemodified)) THEN (DATEDIFF(DATE_ADD(FROM_UNIXTIME(rr.timemodified), INTERVAL p.validFor MONTH) , NOW())) 
    WHEN date(validFrom) > date(FROM_UNIXTIME(rr.timemodified)) THEN (DATEDIFF(DATE_ADD(validFrom, INTERVAL p.validFor MONTH) , NOW()) ) 
END )AS daysLeft, 
IF (rr.timemodified < g.timemodified, (DATEDIFF(DATE_ADD(FROM_UNIXTIME(g.timemodified), INTERVAL p.validFor MONTH) , NOW())- (validFor * 30.48))) as daysLeft

FROM mdl_readtickbox_responses rr 
JOIN mdl_readtickbox r ON r.id = rr.readtickboxid 
JOIN mdl_course c ON c.id = r.course 
JOIN mdl_procedures p ON p.id = c.idnumber 
JOIN mdl_groups_members gm ON gm.userid = rr.userid 
JOIN mdl_groups g ON g.id = gm.groupid 
WHERE rr.userid = '2' 
AND g.courseid = r.course 
AND p.status != 'obsolete' 
ORDER BY p.priority, daysLeft

2 个答案:

答案 0 :(得分:0)

你没有FALSE部分

IF (
        rr.timemodified < g.timemodified, 
        (DATEDIFF(DATE_ADD(FROM_UNIXTIME(g.timemodified), INTERVAL p.validFor MONTH) , NOW())- (validFor * 30.48))
    ) as daysLeft

IF(expr1,expr2,expr3)

  

如果expr1为TRUE(expr1&lt;&gt; 0且expr1&lt;&gt; NULL),则IF()返回   表达式2;否则它返回expr3。

答案 1 :(得分:0)

你忘记了if语句中的其他内容

 IF (rr.timemodified < g.timemodified, (DATEDIFF(DATE_ADD(FROM_UNIXTIME(g.timemodified), INTERVAL p.validFor MONTH) , NOW())- (validFor * 30.48)), *********) as daysLeft
                                                                                                                                                    --->>>>> write your else conditon here