tp触发器的使用

里面主要说的是同一个地址2个不同数据库之前的操作,a数据库里面A表数据有 新增、修改、删除时,去触发下面的函数,然后对b数据库里面的B表做对应的操作(A表B表结构可以说是相同的)

DROP TRIGGER IF EXISTS `add_rma_order`;

DELIMITER ;;
CREATE TRIGGER `add_rma_order` AFTER INSERT ON `tp_user_rma_order_descript` FOR EACH ROW BEGIN
DECLARE oid INT DEFAULT 0;
DECLARE path varchar(100) DEFAULT 'http://www.mypgy.com';
    SELECT rma.id into oid from ptoa.rma_order_descript as rma
    where rma.order_id=new.order_id;
    if(oid>0) THEN
    UPDATE ptoa.rma_order_descript as rma set
     rma.uid=(SELECT oa_id FROM tp_user_info WHERE uid=new.uid),
     rma.addr_front=new.addr_front,rma.addr_last=new.addr_last,rma.do_addr_front=new.do_addr_front,rma.do_addr_last=new.do_addr_last,
     rma.tel=new.tel,rma.bill_img=concat(path,new.bill_img),rma.bank_img=concat(path,new.bank_img),rma.do_supplier=new.do_supplier,rma.to_time=new.to_time,
     rma.free_date=new.free_date,rma.export_bill=concat(path,new.export_bill),rma.in_supplier=new.in_supplier,
     rma.brand_img=concat(path,new.brand_img),
     rma.product_img=concat(path,new.product_img),rma.ex_time=new.ex_time,rma.goods_hs_code=new.goods_hs_code,
     rma.declare_element=new.declare_element,rma.owner=(SELECT staff_name FROM tp_user_info WHERE uid=new.uid),rma.udate=unix_timestamp()
     where rma.order_id=new.order_id;
    ELSE
INSERT INTO ptoa.rma_order_descript(uid,order_id,addr_front,addr_last,do_addr_front,do_addr_last,
tel,bill_img,bank_img,do_supplier,to_time,free_date,export_bill,in_supplier,brand_img,product_img,
ex_time,goods_hs_code,declare_element,owner,cdate)
VALUES((SELECT oa_id FROM tp_user_info WHERE uid=new.uid),new.order_id,
new.addr_front,new.addr_last,new.do_addr_front,new.do_addr_last, new.tel,concat(path,new.bill_img),concat(path,new.bank_img),
      new.do_supplier,
new.to_time,new.free_date,concat(path,new.export_bill),new.in_supplier,concat(path,new.brand_img),concat(path,new.product_img),
new.ex_time,new.goods_hs_code,new.declare_element,(SELECT staff_name FROM tp_user_info WHERE uid=new.uid),unix_timestamp()); 
    END IF;
END
;;

DELIMITER ;



DROP TRIGGER IF EXISTS `edit_rma_order`;
DELIMITER ;;
CREATE TRIGGER `edit_rma_order` AFTER UPDATE ON `tp_user_rma_order_descript` FOR EACH ROW BEGIN
DECLARE oid INT DEFAULT 0;
DECLARE path varchar(100) DEFAULT 'http://www.xtpgy.com';
    SELECT rma.id into oid from ptoa.rma_order_descript as rma
    where rma.order_id=old.order_id;
    if(oid>0) THEN
    UPDATE ptoa.rma_order_descript as rma set
     rma.uid=(SELECT oa_id FROM tp_user_info WHERE uid=old.uid),
     rma.addr_front=new.addr_front,rma.addr_last=new.addr_last,rma.do_addr_front=new.do_addr_front,rma.do_addr_last=new.do_addr_last,
     rma.tel=new.tel,rma.bill_img=concat(path,new.bill_img),rma.bank_img=concat(path,new.bank_img),rma.do_supplier=new.do_supplier,rma.to_time=new.to_time,
     rma.free_date=new.free_date,rma.export_bill=concat(path,new.export_bill),rma.in_supplier=new.in_supplier,rma.brand_img=concat(path,new.brand_img),
     rma.product_img=concat(path,new.product_img),rma.ex_time=new.ex_time,rma.goods_hs_code=new.goods_hs_code,rma.declare_element=new.declare_element,
     rma.owner=(SELECT staff_name FROM tp_user_info WHERE uid=new.uid),rma.udate=unix_timestamp()
     where rma.order_id=old.order_id;
    ELSE
INSERT INTO ptoa.rma_order_descript(uid,order_id,addr_front,addr_last,do_addr_front,do_addr_last,
tel,bill_img,bank_img,do_supplier,to_time,free_date,export_bill,in_supplier,brand_img,product_img,
ex_time,goods_hs_code,declare_element,owner,cdate)
VALUES((SELECT oa_id FROM tp_user_info WHERE uid=old.uid),new.order_id,
new.addr_front,new.addr_last,new.do_addr_front,new.do_addr_last, new.tel,concat(path,new.bill_img),concat(path,new.bank_img),new.do_supplier,
new.to_time,new.free_date,concat(path,new.export_bill),new.in_supplier,concat(path,new.brand_img),concat(path,new.product_img),
new.ex_time,new.goods_hs_code,new.declare_element,(SELECT staff_name FROM tp_user_info WHERE uid=new.uid),unix_timestamp()); 
    END IF;
END
;;

DELIMITER ;



DROP TRIGGER IF EXISTS `del_rma_order`;
DELIMITER ;;
CREATE TRIGGER `del_rma_order` AFTER DELETE ON `tp_user_rma_order_descript` FOR EACH ROW BEGIN
DECLARE oid INT DEFAULT 0;
    SELECT rma.id into oid from ptoa.rma_order_descript as rma
    where rma.order_id=old.order_id;
    if(oid>0)
    THEN
     UPDATE ptoa.rma_order_descript as rma set
     rma.del_author=(SELECT oa_id FROM tp_user_info WHERE uid=old.uid),rma.del_date=NOW(),rma.del_state='已删除'
     where rma.order_id=old.order_id;
    END IF;
END
;;
DELIMITER ;

你可能感兴趣的:(MySQL(储存过程,触发器))