2

我希望它做的是在 B 上插入新行或编辑现有行时,具体的 'last_seen' 列将在 A 上更新。

这是我到目前为止所拥有的:

CREATE TRIGGER insert_records
AFTER UPDATE ON loan_records
FOR EACH ROW
Update  temp_card A
INNER JOIN loan_records B
SET A.last_seen = CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no

还有这个

CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update  temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no

目前这会更新 A 中的所有“last_seen”列,无论它们是否已更新。

4

2 回答 2

2

你不需要JOIN两个表。只需使用UPDATE 语句的NEW.card_noinWHERE子句。

以下是 (AFTER INSERTAFTER UPDATE) 您的触发器的外观

CREATE TRIGGER tg_loan_records_insert
AFTER INSERT ON loan_records
FOR EACH ROW
  UPDATE temp_card 
     SET last_seen = NOW()
   WHERE card_no = NEW.card_no;

CREATE TRIGGER tg_loan_records_update
AFTER UPDATE ON loan_records
FOR EACH ROW
  UPDATE temp_card 
     SET last_seen = NOW()
   WHERE card_no = NEW.card_no;

这是SQLFiddle演示

于 2013-07-26T06:15:51.027 回答
0

你应该只参考当前更新的 id。

CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update  temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no and a.id = OLD.id

更新前的详细使用方法,更新后请参考

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

于 2013-07-26T06:13:21.617 回答