2016-04-30 44 views
0

我想用触发器中每个选择行的值做一个操作,但我不知道如何开始设计这个算法。如何对select中的每一行执行操作?

create trigger trigger_updCompleta 
after update on tableA 
for each row 
begin 

    update tableB 
    set tableB.est='reserved' 
    where tableB.id_e= attr1_from_a_row_of_select and 
      tableB.num_a= attr2_from_a_row_of_select and 
      tableB.num_g= attr3_from_a_row_of_select; 
end// 

这是从中选择要比较的值

select c.id_e, c.num_g, c.num_a, c.t from tableC as c 
    where c.id_r=tableA; 

我的表是这个

CREATE TABLE IF NOT EXISTS tableA(
    id_r INT NOT NULL, 
    id_c INT NOT NULL REFERENCES cliente (id_cliente), 
    completed BOOLEAN NOT NULL, 
    PRIMARY KEY (id_r) 
); 

CREATE TABLE IF NOT EXISTS tableB(
    id_e INT NOT NULL REFERENCES grada (id_e), 
    num_g INT NOT NULL REFERENCES grada (num_g), 
    num_a INT NOT NULL, 
    est ENUM('reserved', 'libre', 'pre-reservado', 'deteriorado') NOT NULL DEFAULT 'libre', 
    PRIMARY KEY (id_e , num_g , num_a) 
); 

CREATE TABLE IF NOT EXISTS tableC(
    id_r INT NOT NULL REFERENCES reserva (id_reserva), 
    id_e INT NOT NULL REFERENCES localidad (id_evento), 
    num_g INT NOT NULL REFERENCES localidad (num_grada), 
    num_a INT NOT NULL REFERENCES localidad (num_asiento), 
    t ENUM('bebe', 'infantil', 'adulto', 'parado', 'jubilado') NOT NULL  REFERENCES precio (t), 
    PRIMARY KEY (id_r , id_e , num_g , num_a) 
); 

我想,当我TableA上更新行,自动将tableB.est的值更改为reserved,对于tableB.id_e,tableB.num_a和tableB.num_g中的每个行都与选择的值c.id_e,c.num_g和c.num_a相同

+0

请问你的表看,结果是什么你正在寻找? –

+0

by'tableB.id_e = attr1_from_a_row_of_select'你的意思是做'tableB.id_e = NEW.attr1' –

+0

不,我的意思是选择返回的atribues之一 –

回答

0

在我解决它为我的自我

begin 

    if new.completed=true then 
    update tableB,(select e.id_e as ev ,e.num_g as grad ,e.num_a as asient,e.t as tip from tableC as e, tableA as r 
          where e.id_r=r.id_r) sel 
    set tableB.e= 'reserved' 
    where tableB.id_e= ev and 
      tableB.num_a= asient and 
      tableB.num_g= grad; 
    ELSE 
    update tableB,(select e.id_e as ev ,e.num_g as grad ,e.num_a as asient,e.t as tip from tableC as e, tableA as r 
          where e.id_r=r.id_r) sel 
    set tableB.e= 'pre-reserved' 
    where tableB.id_e= ev and 
      tableB.num_a= asient and 
      tableB.num_g= grad; 
    end if; 

end// 

最后,我希望这可以帮助任何人

相关问题