向au_online_info插入一条记录,如果au_online_total表中存在相同userid的记录,则total_online,total_score累加online_time,如果不存在就向表au_online_total中一条记录(total_online,total_score为相同值)。
- Create Table: CREATE TABLE `au_online_info` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `userid` bigint(20) NOT NULL,
- `username` varchar(50) NOT NULL,
- `login_time` datetime NOT NULL,
- `logout_time` datetime NOT NULL,
- `online_time` float NOT NULL,
- `login_ip` varchar(50) DEFAULT NULL,
- `memo` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1
- Create Table: CREATE TABLE `au_online_total` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `userid` bigint(20) NOT NULL,
- `username` varchar(50) NOT NULL,
- `total_online` bigint(20) NOT NULL,
- `total_score` bigint(20) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1
- delimiter //
- create trigger au_online_info_total after insert on au_online_info for each row
- Begin
- set @userid:=new.userid;
- set @online_time:=new.online_time;
- If not exists(select 1 from au_online_total where userid= @userid) then
- insert into au_online_total(userid,username,total_online,total_score) select userid,username,sum(online_time),sum(online_time) from au_online_info where userid=@userid;
- else
- update au_online_total set total_online=total_online @online_time,total_score=total_score @online_time where userid=@userid;
- END IF;
- end;
- //
- delimiter ;
- insert into au_online_info(userid,username,online_time) values(2,'jack2',1) ;
阅读(2776) | 评论(0) | 转发(0) |