2016-04-30 73 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// 

最後,我希望這可以幫助任何人

相關問題