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 和东西 cost 是完全不同的值,我无法比较它们,它们总是不同的。

当我使用

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

@编辑

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;
$$
4

2 回答 2

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;
$$
于 2012-12-03T15:11:32.537 回答
3

试试这个代码...

**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;

分隔符 ///

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;

///

分隔符;

于 2014-03-06T10:20:59.170 回答