当一行插入tableb
时,如果没有任何存储过程或其他内容,如何使用tablea
中的值自动更新第二个表tablea
。
例如,如果要在此处将一行插入到表格中,我希望使用tableb
itemId
tablea
更新itemTempId
中的所有行tableb
中的1}}与tablea
两个表都是MyIsam类型
插入表格之前
tablea
"id" "itemId" "itemTempId" "itemName"
- - - -
tableb
"id" "itemId" "itemTempId" "itemImageName"
"1" \N "T001" "front.jpg"
"2" \N "T001" "side-left.jpg"
"3" \N "T001" "side-right.jpg"
"4" \N "T001" "back.jpg"
插入表格后
tablea
"id" "itemId" "itemTempId" "itemName"
"1" "US01" "T001" "Samsung Galaxy Note 5"
tableb //updated with the itemId from tablea where itemTempId are the same
"id" "itemId" "itemTempId" "itemImageName"
"1" "US01" "T001" "front.jpg"
"2" "US01" "T001" "side-left.jpg"
"3" "US01" "T001" "side-right.jpg"
"4" "US01" "T001" "back.jpg"
答案 0 :(得分:1)
在TRIGGER
中插入一行后,您可以使用tableb
更新tablea
:
CREATE TRIGGER new_trigger
AFTER INSERT ON tablea
FOR EACH ROW
UPDATE tableb SET itemId = NEW.itemID WHERE itemTempID = NEW.itemTempID;
我认为如果没有TRIGGER
你就无法做到这一点,就像这样,它真的很简单。