2012-12-03 101 views
1

當前你好,我的觸發器更新表更新,我需要改變它只在特定列更改時觸發。列更改觸發器更新

代碼: 創建觸發器錢things

for each row 
begin 
UPDATE `current` c 
INNER JOIN things t ON c.id2 = t.id2 
INNER JOIN dude_base d ON d.id1 = c.is1 
SET c.`curr_cash` = t.thing_cost * d.salary/100; 
end; 
$$ 

更新後,我需要改變,因此它會打開,當有來自東西「thing_cost」更新。

@edit 我必須更新curr_cash和東西的成本是完全不同的值,我無法比較它們,它們將永遠是不同的。

當我用

if NEW.things_cost <> OLD.things_cost 

我獲得以下錯誤:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 10 

@edit

create trigger wys_sk_b after update on `dude_base` 
for each row 
begin 
if NEW.salary <> OLD.salary 
then 
UPDATE `current` s 
INNER JOIN things u ON s.id_thing = u.id_thing 
INNER JOIN dude_base b ON b.id = s.id 
SET s.`curr_cash` = u.thing_cost * b.salary/ 100; 
end if; 
$$ 
+1

WHERE'current'.'thing_cost' ='things'.'thing_cost' –

回答

1

試試這個代碼...

**create table sales** (orderno INT, sale INT,empsalary int, ts TIMESTAMP); 

**create table history** (updated varchar(20), oldvalue INT,newvalue INT); 

INSERT INTO **sales** (orderno,sale,empsalary) VALUES(1,700,7000); 

INSERT INTO **sales** (orderno,sale,empsalary) VALUES(2,800,8000); 

INSERT INTO **sales** (orderno,sale,empsalary) VALUES(3,900,9000); 


DROP TRIGGER test.instrigger; 

DELIMITER ///

CREATE TRIGGER test.instrigger AFTER UPDATE ON sales 
FOR EACH ROW 

BEGIN 

    IF NEW.sale <> OLD.sale THEN 
     INSERT INTO history (updated, oldvalue, newvalue) VALUES('sale', OLD.sale,NEW.sale); 

    END IF; 
    IF NEW.empsalary <> OLD.empsalary THEN 
     INSERT INTO history (updated, oldvalue, newvalue) VALUES('empsalary', OLD.empsalary,NEW.empsalary); 
    END IF; 
END; 

///

DELIMITER;

3
delimiter $$ 
create trigger wys_sk_b after update on `dude_base` 
for each row 
begin 
    if NEW.salary <> OLD.salary 
    then 
    UPDATE `current` s 
    INNER JOIN things u ON s.id_thing = u.id_thing 
    INNER JOIN dude_base b ON b.id = s.id 
    SET s.`curr_cash` = u.thing_cost * b.salary/ 100; 
    end if; 
end; 
$$ 
+0

它發送我的語法錯誤接近結束... nothign具體:/ – trinny

+0

的語法是正確的。檢查觸發器周圍的分隔符。 – Devart

+0

仍然不能正常工作,在「end if;」結束時給出錯誤結果線。 我編輯過的文章,也許你會用逗號看到一些錯誤,或者; – trinny