代码之家  ›  专栏  ›  技术社区  ›  Felix

MySQL触发器问题

  •  0
  • Felix  · 技术社区  · 16 年前

    delimiter //
    
    create trigger companyA_phones after insert on customer
    
    for each row
    
    begin
    
       set @phone = (select new.phone from customer where new.phone regexp '^0416');
    
       if (@phone) then
    
          insert into company_A(new.id, @phone);
    
       end if;
    
    end//
    
    1 回复  |  直到 16 年前
        1
  •  3
  •   Tom    16 年前

    你没有完全弄清楚你遇到了什么麻烦,但我认为你不需要选择像那样的新值——因为它们已经为你提供了。尝试以下操作:

    CREATE TRIGGER companyA_phones
    AFTER INSERT ON customer
    FOR EACH ROW
    BEGIN
      IF (new.phone REGEXP '^0416' AND new.id IS NOT NULL) THEN
        INSERT INTO company_A (customerid, phone)
             VALUES (new.id, new.phone);
      END IF;
    END