要使用orderinfo
基于 a 的客户姓名和地址的当前值进行更新CustomerID
,请使用 a UPDATE
JOIN
:
UPDATE
orderinfo
JOIN customerinfo ON orderinfo.CustomerID = customerinfo.CustomerID
SET
OrderCustomerName = CustomerName,
OrderCustomerAddress = CustomerAddress
WHERE OrderID = <some_order_id>
如果您想“冻结”与特定订单关联的客户信息,即使客户稍后在全局范围内更改该信息,这也很有用。如果我理解正确,那是你的意图。
您也可以使用桌子AFTER INSERT
上的触发器来执行此操作。orderinfo
然后您无需执行单独的查询:
CREATE TRIGGER update_order_cust_info AFTER INSERT ON orderinfo
FOR EACH ROW
BEGIN
UPDATE
orderinfo
JOIN customerinfo ON orderinfo.CustomerID = customerinfo.CustomerID
SET
NEW.OrderCustomerName = CustomerName,
NEW.OrderCustomerAddress = CustomerAddress
WHERE OrderID = NEW.OrderID
END