一个关于MySQL触发器的问题,要求是。在一个表1执行一条update语句后,统计与该语句的id相同信息的条数,然后计算出条数的一半数目。
然后再判断该表1中id相同条目的中的state字段,是否全部相同。全部相同则更改同数据库下的另一个表2中的与其有相同id的信息(唯一)的state字段的值。由于本人对MySQL的触发器的操作不熟,所以求大神们指点。
这个是在sql2005中的写法,(应该是没有错误的,不知道写错。就是要在MySQL中实现这个触发器的功能)CREATE TRIGGER changestate
on applystate
after update
for each row
as declare @applyid int, @count int, @count2 int
select @applyid=applyid from INSERTED
SELECT @count=count(*) from y.applystate where applystate.applyid = @applyid
select @count2=count(*) from y.applystate where applystate.applyid = @applyid and state=1
if @count2>=@count1
print 'ok'
然后再判断该表1中id相同条目的中的state字段,是否全部相同。全部相同则更改同数据库下的另一个表2中的与其有相同id的信息(唯一)的state字段的值。由于本人对MySQL的触发器的操作不熟,所以求大神们指点。
这个是在sql2005中的写法,(应该是没有错误的,不知道写错。就是要在MySQL中实现这个触发器的功能)CREATE TRIGGER changestate
on applystate
after update
for each row
as declare @applyid int, @count int, @count2 int
select @applyid=applyid from INSERTED
SELECT @count=count(*) from y.applystate where applystate.applyid = @applyid
select @count2=count(*) from y.applystate where applystate.applyid = @applyid and state=1
if @count2>=@count1
print 'ok'
解决方案 »
- pgAdmin数据导出问题
- 如何取当前日期和计算两个日期之间的天数?
- 很奇怪的问题
- 怎么让mysql同时支持繁体和简体字???????
- 向高手们请教一个SQL语句!
- MySQL里面有没有日期函数 将字符”Thu, 7 Aug 2003 05:42:23 +0800“ 转换成"yyyy-mm-dd hh:mm:ss"格式日期?
- 数据库老是出现索引损坏,怎么样避免这种损坏?
- Help!!!!!A MS access problem!
- delete in() 操作 子查询报错时删除全表
- 新手,sql查询语句求帮助
- 需要大量在MySQL数据库中实现独立于操作系统的目录(或者说树)的资料
- 如何创建一个视图,需要引用两个表的字段
FOR EACH ROW
BEGINdeclare applyid int;
declare count1 int;
declare count2 int;
set applyid=(select applyid from applystate);
set count1=(select count(*) from applystate where applyid = applyid);
set count2=(select count(*) from applystate where applyid = applyid and applystate.state=1);if count2>=count1 THEN
update app_menu set name='12222' where id=1;
END IF ;END
FOR EACH ROW BEGIN
declare v_applyid int;
declare v_count int;
declare v_ount2 int;
SELECT count(*) into v_count from y.applystate where applystate.applyid = new.applyid;
select count(*) into v_ount2 from y.applystate where applystate.applyid = new.applyid and state=1;
END;
|delimiter ;
delimiter $$
CREATE TRIGGER changestate after update ON applystate
FOR EACH ROW BEGIN
declare v_applyid int;
declare v_count int;
declare v_ount2 int;
SELECT count(*) into v_count from y.applystate where applystate.applyid = new.applyid;
select count(*) into v_ount2 from y.applystate where applystate.applyid = new.applyid and state=1;
if v_count=v_count2 then
你的操作;
end if;
END$$
delimiter ;