如果否则不能在phpmyadmin触发器中工作

时间:2015-03-16 10:13:33

标签: php mysql if-statement triggers phpmyadmin

所以我在phpmyadmin中使用MySQL触发器。这是我scorm_track表的触发器:

UPDATE userbatch 
LEFT JOIN scorm_track 
ON      
        (userbatch.user_id = scorm_track.userid)
LEFT JOIN trainingobject
ON      
        (scorm_track.tobjid = trainingobject.id)
IF scorm_track.element = 'cmi.core.lesson_status' THEN
    SET userbatch.to_completion = scorm_track.value,
        userbatch.trainingcompleted = scorm_track.timemodified;
ELSEIF scorm_track.element = 'cmi.core.score.raw' THEN
    SET userbatch.comment = scorm_track.value;
ENDIF;
WHERE (userbatch.batch_id = trainingobject.batchid) AND 
        scorm_track.id=NEW.id AND 
        scorm_track.element = 'cmi.core.lesson_status' OR scorm_track.element = 'cmi.core.score.raw'

但是它显示以下错误:“MySQL说#1064 - 你的SQL语法有错误......”

在添加IF ELSE语句后,此问题开始发生。之前工作正常。任何人都可以建议为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

这是因为加入时更新语法错误。 if-else用法在这里是错误的。查询应该是

update userbatch ub
left join scorm_track st on ub.user_id = st.userid
left join trainingobject tob on tob.id = st.tobjid
set
ub.to_completion = 
 case 
   when st.element = 'cmi.core.lesson_status' then st.value 
 end ,
ub.to_completion = 
 case when st.element = 'cmi.core.lesson_status' then st.value 
end, 
ub.trainingcompleted = 
 case when st.element = 'cmi.core.lesson_status' then st.timemodified 
end, 
ub.comment = 
case when st.element = 'cmi.core.score.raw' then st.value 
end
where 
ub.batch_id = tob.batchid
and st.id = new.id 
and ( st.element = 'cmi.core.lesson_status' or st.element = 'cmi.core.score.raw )