我试图设置一个If / Then语句来确定插入"课程"我的数据库中的表。第一个INSERT INTO将插入限制为6行,第二个将其限制为7。
代码如下所示:
If @courseprogram = 'PharmTech' Then
INSERT INTO course (fameid, course_id, enroll_status)
SELECT @fameid fame_id, course_id, 'S' enroll_status FROM calendar
WHERE startdate >= @startdate AND program = @courseprogram ORDER BY startdate LIMIT 6;
Elseif @fullprogram = 'Medical Assisting'
INSERT INTO course (fameid, course_id, enroll_status)
SELECT @fameid fame_id, course_id, 'S' enroll_status FROM calendar
WHERE startdate >= @startdate AND program = @courseprogram ORDER BY startdate LIMIT 7;
End if
当我尝试运行它时,我收到以下错误消息:
错误代码:1064。您的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 语法使用附近'如果@courseprogram =' PharmTech'然后INSERT INTO 当然(fameid,course_id,enr' at line 1
有谁知道这里可能出现什么问题?
另外,我已经在这个问题的几篇文章中读过,应该在MySQL中避免使用IF / THEN语句。任何人都可以解释为什么会这样,以及如何处理这个问题?
答案 0 :(得分:1)
INSERT INTO course (fameid, course_id, enroll_status)
(SELECT @fameid as fame_id, course_id, 'S' as enroll_status
FROM calendar
WHERE @courseprogram = 'PharmTech' AND
startdate >= @startdate AND
program = @courseprogram
ORDER BY startdate
LIMIT 6
) UNION ALL
(SELECT @fameid as fame_id, course_id, 'S' as enroll_status
FROM calendar
WHERE @fullprogram = 'Medical Assisting' AND
startdate >= @startdate AND
program = @courseprogram
ORDER BY startdate
LIMIT 7
);
我并不认为这是优雅。仅仅是它解决了你的问题。
注意:我猜测第二个子查询中的条件@fullprogram = 'Medical Assisting'
应该是@courseprogram = 'Medical Assisting'
。