您好,欢迎访问一九零五行业门户网

一个简单MySQL触发器例子_MySQL

bitscn.com
一个简单mysql触发器例子
有这样一个需求,更新某张表的某个字段时,要先判断,如果新值比表中老值小,则将老值和新值相加,然后更新;否则正常更新。考虑用mysql的触发器实现,更新时触发。
下面是具体的sql, 一看便知。
[sql] 
-- 删除触发器  
drop trigger trigger_ads;
-- 创建触发器  
delimiter //  
create trigger trigger_ads before update on stats_ads  
for each row  
begin      
    if old.view > new.view then    
        set new.view = old.view + new.view;  
    end if;  
    if old.view_unique > new.view_unique then    
    set new.view_unique = old.view_unique + new.view_unique;  
    end if;  
    if old.click > new.click then    
    set new.click = old.click + new.click;  
    end if;  
    if old.click_unique > new.click_unique then    
    set new.click_unique = old.click_unique + new.click_unique;  
    end if;  
    if old.start > new.start then    
    set new.start = old.start + new.start;  
    end if;  
    if old.start_unique > new.start_unique then    
    set new.start_unique = old.start_unique + new.start_unique;  
    end if;  
    if old.landing > new.landing then    
    set new.landing = old.landing + new.landing;  
    end if;  
    if old.landing_unique > new.landing_unique then    
    set new.landing_unique = old.landing_unique + new.landing_unique;  
    end if;  
end;  
//  
最后可以通过下面sql语句查看所有触发器:
[sql] 
-- 查看mysql触发器  
select * from information_schema.`triggers`;
bitscn.com
其它类似信息

推荐信息