mysql - I want to increase countoftask of category when a task of that category is inserted in task table -
create trigger tr_task_forinsert after insert on task each row begin set @count = (select countoftask category catid = new.catid , userid=new.userid); update category set countoftask = @count+1 catid=new.catid , userid=new.userid; end
where error?
task table
+----------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +----------+--------------+------+-----+---------+----------------+ | taskid | int(11) | no | pri | null | auto_increment | | taskname | varchar(255) | yes | | null | | | catid | int(11) | yes | mul | null | | | userid | int(11) | yes | mul | null | | | taskdate | date | yes | | null | | | tasktime | varchar(255) | yes | | null | | +----------+--------------+------+-----+---------+----------------+
category table
+-------------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-------------+--------------+------+-----+---------+----------------+ | catid | int(11) | no | pri | null | auto_increment | | catname | varchar(255) | yes | | null | | | userid | int(11) | no | pri | 0 | | | countoftask | int(11) | yes | | null | | +-------------+--------------+------+-----+---------+----------------+
you don't need fetch value variable, won't cause error. simpler form:
delimiter $$ create trigger tr_task_forinsert after insert on task each row begin update category set countoftask = countoftask + 1 catid = new.catid , userid = new.userid; end; $$ delimiter ;
your problem might delimiters.
Comments
Post a Comment