mysql - 使用mysql更新表贡献计算的触发器

标签 mysql triggers calculation

我需要根据每个开发人员的项目计算贡献

Contribution table
-------------------------------------------------------------
| id | projected | developer id | total hours | contribution|
-------------------------------------------------------------
| 1  | 1         |  1           |      25     |             |
-------------------------------------------------------------
| 2  | 1         |  2           |      75     |             |
-------------------------------------------------------------
| 3  | 2         |  1           |      10     |             |
-------------------------------------------------------------

插入和更新后需要使用触发器更新同一个表 预期结果

  Contribution table
    -------------------------------------------------------------
    | id | projected | developer id | total hours | contribution|
    -------------------------------------------------------------
    | 1  | 1         |  1           |      25     |   25%       |
    ------------------------------------------------------------
    | 2  | 1         |  2           |      75     |   75%       |
    -------------------------------------------------------------
    | 3  | 2         |  1           |      10     |   100%      |
    -------------------------------------------------------------

calculation for the getting contribution

project 1 :
total hours = 25 + 75 = 100 contribution per developer = 25/100*100 = 25%

我需要一个触发器来获得这个结果:但不知道如何获得这个

这是我的触发器没有收到错误,但贡献计算不正确

 CREATE TRIGGER `update_contribution` AFTER INSERT ON `tasks`
 FOR EACH ROW BEGIN 
 IF NOT EXISTS 
 (SELECT p_id ,d_id 
  FROM contribution 
  WHERE 
  p_id = NEW.p_id 
  AND 
  d_id = NEW.d_id) 
  THEN

 SET @old_total_dev_hours = (SELECT SUM(total_hours)             
                       FROM contribution 
                       WHERE p_id = NEW.p_id
                       GROUP BY p_id);


 SET @total_hours1 = (SELECT (total_hours)             
                       FROM contribution 
                       WHERE d_id = NEW.d_id AND p_id = NEW.p_id
                       );

  SET @dev_con =  @total_hours1/@old_total_dev_hours*100 ;

  SET @total_hours =  new.hours + new.overtime;
  INSERT INTO contribution
    ( p_id,
     d_id,
     hours,
     overtime,
     total_hours,
     contribution
    )
   VALUES
   (NEW.p_id,
    NEW.d_id,
    NEW.hours,
    NEW.overtime,
      @total_hours ,
    @dev_con
   );
 ELSE
   UPDATE contribution 
   SET 
   hours = hours + NEW.hours , 
   overtime = overtime + NEW.overtime, 
   total_hours = hours + overtime,
   contribution = @dev_con
   WHERE 
   p_id = NEW.p_id 
   AND 
   d_id = NEW.d_id;
   END IF;
   END

This is my code in this code other calculation are working fine contribution is not getting correctly!!

最佳答案

这是一个更新的触发器,它将取代您上一个问题中的触发器。它计算 Total_hours 和贡献百分比的两个值。

DELIMITER |

CREATE TRIGGER update_hours AFTER INSERT ON tasks
FOR EACH ROW 
BEGIN
    SET @old_total_dev_hours = (SELECT SUM(hours + overtime)
        FROM contribution 
        WHERE p_id == new.p_id && d_id == new.d_id 
        GROUP BY p_id,d_id);
    SET @old_total_hours = (SELECT SUM(hours + overtime)
        FROM contribution 
        WHERE p_id == new.p_id
        GROUP BY p_id);

    SET @total_hours = @old_total_dev_hours + new.hours + new.overtime;
    SET @contrib_percent = (@old_total_dev_hours / @old_total_hours) * 100;

    INSERT INTO contribution
    ( p_id,
    d_id,
    hours,
    overtime,
    total_hours,
    contribution )
    VALUES
    ( 
        NEW.p_id,
        NEW.d_id,
        NEW.hours,
        NEW.overtime,
        @total_hours,
        @contrib_percent
    );
END|

DELIMITER ;

关于mysql - 使用mysql更新表贡献计算的触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50095234/

相关文章:

sql - 在连接表中选择所有不满足特定条件的记录

MySQL LEFT JOIN 在列表中

sql-server - 循环触发器内的所有列

mysql - 如何将变量设置为触发器 MYSQL 中存储过程的结果?

sql-server - 为什么 FOR 触发器不在操作之前运行?

javascript - 计算器高度宽度

php - 显示整个数据库的多字段搜索表单

php - 如何使用连续的服务器端处理来制作不断更新数据库信息的新闻源?

lua - 以更高的精度计算 Lua 中的值

r - 将数字分成 0-1 之间的等间隔区间