mysql trigger根据条件在插入后更新一行

时间:2014-11-01 14:41:24

标签: mysql triggers

我正在编写一个MySQL触发器。其中我想根据分支ID更新branch_form_no和自动增量表单号。我该怎么办?

我的表hcm_staffing_applicantform包含列id,applicant_name等,branch_id,branch_form_no

CREATE TRIGGER update_branch_appform
AFTER INSERT ON branch_form_no
FOR EACH ROW
BEGIN
IF branch_id = 1 then
    UPDATE hcm_staffing_applicantform
    SET branch_form_no = ((SELECT max(branch_form_no) from hcm_staffing_applicantform where hcm_staffing_applicantform.branch_id=1) + 1)
    WHERE hcm_staffing_applicantform.branch_form_no = NEW.branch_form_no;
ELSEIF branch_id = 2 THEN
    UPDATE hcm_staffing_applicantform
    SET branch_form_no = ((SELECT max(branch_form_no) from hcm_staffing_applicantform where hcm_staffing_applicantform.branch_id=1) + 1)
    WHERE hcm_staffing_applicantform.branch_form_no = NEW.branch_form_no;
ENDIF

我也试过这个

DELIMITER $$
CREATE TRIGGER update_branch_appform
AFTER INSERT ON hcm_staffing_applicantform
FOR EACH ROW
BEGIN
    UPDATE hcm_staffing_applicantform
    SET branch_form_no = ((SELECT max(branch_form_no) from hcm_staffing_applicantform where hcm_staffing_applicantform.branch_id= NEW.BRANCH_ID) + 1)
    WHERE hcm_staffing_applicantform.branch_form_no = NEW.branch_form_no;
END; $$
DELIMITER ;

但不工作.....任何人?请

0 个答案:

没有答案
相关问题