简体   繁体   中英

Updating other table column after insert in MySQL

I have these two table called "cases" and attendance respectively which has four columns:

cases-
 id  empid     reaction    date_t
 1   EMP12654 interested   2017-09-22

 attendance-

id empid    logintime logouttime   date_t      flag  workinghours call_att
1  EMP12654 00:14:49   05:14:49    2017-09-18  set      6          1

What I want to do is create a trigger on cases table that updates call_att column of attendance table with number of entries in reaction column of cases table, this is what I have tried so far

    CREATE DEFINER=`root`@`localhost` TRIGGER `number_call` 
AFTER INSERT ON `cases` FOR EACH ROW 
BEGIN UPDATE attendance set call_att=call_att +1 
WHERE empid=new.empid AND date_t=new.date_t; END

But that doesn't seem to work. I am quite new to triggers.

try this

CREATE TRIGGER number_call 
    AFTER INSERT ON cases
    FOR EACH ROW 
BEGIN
UPDATE attendance set call_att=(select count(*) from cases where empid=NEW.empid )
date_t=NEW.date_t;
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM