2011-08-19 87 views
0

聽起來這麼簡單......我花了幾個小時才完成了第一部分工作,這是對不同數據庫的mysql觸發器。現在我想變得聰明,並加入一對夫婦的桌子。MySQL觸發器將數據插入到不同的數據庫中

我有兩個主表PROJ和COMP。兩者共享id。當PROJ插入時,我想插入一些NEW.PROJ信息和一些COMP信息到db.table axis.axis_data中的單個行中。一些人請幫我用TRIGGER做一個SELECT...INSERT。我可能會在這一個

我的工作觸發器。

DELIMITER $$ 

DROP TRIGGER IF EXISTS `rate_data_trigger` $$ 

CREATE TRIGGER rate_data_trigger 
    BEFORE INSERT on PROJ FOR EACH ROW 
    BEGIN 

     INSERT INTO axis.axis_data 
      (projinfo_table_id, rate_user, name, 
      property_owner, property_address, property_city, 
      property_state, property_zip, property_phone, 
      rating_date, rating_type, rating_reason, rating_number 
      ) 

     VALUES 
      (NEW.id, user(), NEW.BLGNAME, 
      NEW.POWNER, NEW.STREET, NEW.CITY, 
      NEW.STATE, NEW.ZIP, NEW.PHONE, 
      NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO 
      ); 
    END$$ 
DELIMITER ; 

回答

1

只需使用以下語法在SELECT語句:

INSERT INTO axis.axis_data 
     (projinfo_table_id, rate_user, name, 
     property_owner, property_address, property_city, 
     property_state, property_zip, property_phone, 
     rating_date, rating_type, rating_reason, rating_number, 
     field1, field2 
     ) 

    SELECT NEW.id, user(), NEW.BLGNAME, 
     NEW.POWNER, NEW.STREET, NEW.CITY, 
     NEW.STATE, NEW.ZIP, NEW.PHONE, 
     NEW.RATDATE, NEW.RATTYPE, NEW.RATREAS, NEW.RATNGNO, 
     c.field1, c.field2 
    FROM COMP c WHERE c.id = NEW.id 

如果COMP並不總是必須在PROJ相應的記錄,你可以使用SELECT ... FROM DUAL LEFT JOIN COMP c ON c.id = NEW.id

+0

是啊 - 我稍微調整了這個,但是謝謝! – rh0dium