MySQL trigger to update a field to the value of id

前端 未结 6 606
臣服心动
臣服心动 2020-11-27 21:21

I would like to have a trigger to perform following operation for inserted records:

 # pseudocode
 if new.group_id is null
    set new.group_id = new.id
 els         


        
6条回答
  •  无人及你
    2020-11-27 21:49

    I believe that this will work for you

    I have two tables

    test_b: a_id, value
    test_c: a_id, value
    

    And here is a trigger on the insert of test b. It checks to see if a_id is null and if it is it inserts 0

    CREATE TRIGGER test_b AFTER INSERT ON test_b
      FOR EACH ROW 
        INSERT INTO test_c (a_id, value) VALUES (IFNULL(NEW.a_id, 0),NEW.value)
    

提交回复
热议问题