每当更新或添加第二个表行时,需要自动更新第一个表的列值

时间:2015-03-28 11:44:17

标签: mysql sql

每当更新或添加第二个表行时,需要自动更新第一个表的列值。

我有两张表CcnCSRCsrTimeReporting

mysql> select * from CcnCSR;      
 select * from CsrTimeReporting;

+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+
| CSRNumber | Slogan                                  | Severity | Customer    | Status    | CreatedDate | CcnQDate   | TTS        | IAdate     | TotalTimeReported | Remark |
+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+
|   2718122 | DIAMETER LINK FLICKS BETWEEN GGSN-CCN38 | High     | IdeaIndia   | NULL      | 2105-03-04  | 2105-03-03 | 2015-03-28 | 2105-03-13 |              NULL | NA     |
|   2718133 | Auto ZR                                 | High     | AirtelIndia | Analysing | 2105-03-20  | 2105-03-23 | 2105-04-10 | NULL       |              NULL | NULL   |
+-----------+-----------------------------------------+----------+-------------+-----------+-------------+------------+------------+------------+-------------------+--------+

2行(0.00秒)

+-----------+-------+------------------+--------------+-------+
| CSRNumber | SeqId | TimeReportedDate | TimeReported | Shift |
+-----------+-------+------------------+--------------+-------+
|   2718122 |     1 | 2015-03-15       |            8 | NULL  |
|   2718122 |     2 | 2105-03-03       |          0.5 | NULL  |
|   2718122 |     3 | 2105-03-03       |            3 | NULL  |
|   2718122 |     4 | 2105-03-03       |          4.5 | NULL  |
|   2718122 |     5 | 2105-03-03       |         5.25 | NULL  |
|   2718122 |     6 | 2105-03-05       |         7.25 | NULL  |
|   2718133 |     8 | 2015-03-30       |            2 | NULL  |
|   2718133 |     9 | 2015-03-31       |          2.5 | NULL  |
|   2718133 |    10 | 2015-03-29       |          3.5 | NULL  |
+-----------+-------+------------------+--------------+-------+

9行(0.00秒)

CcnCSR中有“TotalTimeReported”列,只要更新第二个表“TimeReported”或添加新行,就需要自动更新此列值。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以这样做

create trigger trigger_name
on CsrTimeReporting
for insert
as
begin
declare 
@time_reported decimal,
@CSR_Number nvarchar(50) -- select data type as you need

select @CSR_Number = CSRNumber , @time_reported = TimeReportedDate from 
CsrTimeReporting              

Update CcnCSR set TotalTimeReported = @time_reported where 
CSRNumber = @CSR_Number

答案 1 :(得分:0)

您可以使用多个触发器执行此操作,一个用于insertupdatedelete

delimiter //
create trigger trig:csrTimeReport_insert on CsrTimeReporting
    after insert for each row
begin
    update CcnCSR
        set TotalTimeReported = TotalTimeReported + new.TimeReported
        where CcnCSR.CSRNumber = new.CSRNumber;
end;//

create trigger trig:csrTimeReport_update on CsrTimeReporting
    after update for each row
begin
    update CcnCSR
        set TotalTimeReported = TotalTimeReported + new.TimeReported - old.TimeReported
        where CcnCSR.CSRNumber = new.CSRNumber;
end;//

create trigger trig:csrTimeReport_delete on CsrTimeReporting
    after delete for each row
begin
    update CcnCSR
        set TotalTimeReported = TotalTimeReported +  - old.TimeReported
        where CcnCSR.CSRNumber = old.CSRNumber;
end;//
delimiter ;