0

This is my schema.

table_products - product_id, product_name, product_description, product_image_path, brand_id, available.
table_brands - brand_id, brand_name
table_categories - category_id, category_name
table_product_category_mapping - product_id, category_id 

Data is already filled in tables.

Now Because of new requirement i want to create a trigger to keep track of count of product in each brand and category. so i created a table:

table_product_count - brand_id, category_id, count //under this brand and category these many products are there.

DELIMITER $$ CREATE TRIGGER inc_count AFTER INSERT ON table_products FOR EACH ROW BEGIN UPDATE table_product_count SET count = count + 1 WHERE brand_id = NEW.brand_id; END $$ DELIMITER ;

DELIMITER $$ CREATE TRIGGER inc_count2 AFTER INSERT ON table_products FOR EACH ROW BEGIN UPDATE table_product_count SET count = count + 1 WHERE brand_id = NEW.brand_id; END $$ DELIMITER ;

How to fill count entries in table_product_count table for which(product) data already entered.

1 Answer 1

0

This trigger is working if you have all the brand_id, category_id in table_product_count table:

DELIMITER $$ CREATE TRIGGER inc_count AFTER INSERT ON test.table_products FOR EACH ROW BEGIN UPDATE table_product_count SET COUNT = COUNT + 1 WHERE brand_id = NEW.brand_id; END $$ DELIMITER ;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.