MySQL trigger after insert and after update

后端 未结 2 1640
忘掉有多难
忘掉有多难 2021-01-15 04:22

I have two tables with one named att as follows

CREATE TABLE att (
  SID varchar(50) NOT NULL, 
  CID varchar(50) NOT NULL, 
  Date date NOT         


        
2条回答
  •  感动是毒
    2021-01-15 05:23

    DELIMITER $$
    
    CREATE TRIGGER `att_up` AFTER UPDATE ON `attentance`
    FOR EACH ROW
    
    BEGIN
    
    SET @zeros = 0;
    SET @ones = 0;
    SET @total = 0;
    SET @atted = 0;
    
    
    (SELECT (8-SUM(h1)+SUM(h2)+SUM(h3)+SUM(h4)+SUM(h5)+SUM(h6)+SUM(h7)+SUM(h8)) INTO @zeros FROM attentance WHERE StudID=NEW.StudID);
    (SELECT SUM(h1+h2+h3+h4+h5+h6+h7+h8) INTO @ones FROM attentance WHERE StudID=NEW.StudID);
    (SELECT (SUM(8-(h1+h2+h3+h4+h5+h6+h7+h8))+ SUM(h1+h2+h3+h4+h5+h6+h7+h8)) INTO @total FROM attentance WHERE StudID=NEW.StudID);
    
    SELECT (((@zeros-@ones)/@total)/100) INTO @atted FROM (SELECT 1) AS x;
    
    END$$
    

提交回复
热议问题