From 633955d95fd0e85f0380bd9c63211fa4859c8eaa Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Thu, 7 Mar 2024 22:40:30 +0100 Subject: [PATCH] Add trigger to automatically calculate subtotal price of cart --- shop.sql | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/shop.sql b/shop.sql index b085cde..2cbea62 100644 --- a/shop.sql +++ b/shop.sql @@ -130,4 +130,18 @@ begin where id = old.cart_id; end; // -delimiter; \ No newline at end of file +delimiter; + +delimiter // +create trigger calculate_price_subtotal +before insert on cart_item +for each row +begin + set new.price_subtotal = ( + select new.count * p.price_pc + from product p + where p.id = new.product_id + ); +END; +// +DELIMITER ; \ No newline at end of file