I have three tables:
Product(pid, pname, pbrand, pprice)
ShoppingCart(cartid, cid, active, totalprice) ShoppingCart.cid references to Customer.cid
CartItem(cartid, pid, iprice, iquantity) CartItem.cartid references ShoppingCart.cartid, CartId.pid references Product.pid
if the product price in Table Product is updated, it should first update all carts(CartItem) that contain that product, then update the totalprice calculated in the related shoppingcart.
I can finish the first part and I expect the second task could work like
delimiter //
create trigger update_prodprice after update on product
for each row
begin
update cartitem set iprice=new.pprice where pid=new.pid
and cartid in (select cartid from shoppingcart where active=True);
update shoppingcart set totalprice=sum(cartitem.iprice*cartitem.iquantity)
where active=True and cartid=cartitem.cartid;
end //
delimiter ;
But the second update does not work because of
"ERROR 1054 (42S22): Unknown column 'cartitem.cartid' in 'where clause'"