0

我已经在我的数据库中成功定义了以下过程,但不知何故它没有按预期工作。

CREATE PROCEDURE `mod_prefer`(IN sid INT)
BEGIN
declare s1 int;
declare s2 int;
SELECT SUM(c_load) INTO s1 FROM course WHERE id IN (SELECT course_id FROM prefer WHERE staff_id = sid) AND sem = 1;
SELECT SUM(c_load) INTO s2 FROM course WHERE id IN (SELECT course_id FROM prefer WHERE staff_id = sid) AND sem = 2;
IF s1>s2 THEN
UPDATE staff SET sem = 1 WHERE id = sid;
ELSE
UPDATE staff SET sem = 2 WHERE id = sid;
END IF;
END

使用触发器调用上述过程,如下所示(它是插入后触发器):

CALL mod_prefer(new.staff_id);

结果是,程序没有sem为给定的工作人员设置值,而是将所有 sem 值更新为 2,而不管逻辑应该如何工作,即即使 sem 的值实际上应该是 1。

请帮忙。

4

1 回答 1

0

试试这个代码 -

SELECT
  SUM(IF(sem = 1, c_load, NULL)), SUM(IF(sem = 2, c_load, NULL)) s2
  INTO s1, s2
FROM course c
  JOIN prefer p
    ON p.course_id = c.id
  WHERE staff_id = sid;

UPDATE staff SET sem = IF(s1 > s2, 1, 2) WHERE id = sid;
于 2012-09-26T07:02:27.423 回答